Language syntax

Java while statement

Use a pre-test loop when work may be unnecessary initially. A false initial condition skips the body.

Formal Syntax & Structure

java syntaxjava
while (condition) { statements }

Working Example

Example Snippetjava
public class Main {    public static void main(String[] args) {        int stock = 2;        while (stock > 0) { stock--; }        System.out.println(stock);    }}
Java Reference