x

Java – While loop

PREV     NEXT

Java While loop executes a set of statements repeatedly until a given condition remains true.


Syntax for Java While loop:

while(condition)

{           //statements

}

Example:

Output:

value:5

value:4

value:3

value:2


Value:1

In the above program, the value of i is printed until it is not equal to zero. When the value of i reaches 0, the execution of the loop is terminated.

Infinite loop:

When we explicitly mention the condition value as true in the while loop, then it repeats endlessly. If we would like to break the infinite loop in between, we can use break; statement.

while(true)

{

//statements

}

PREV     NEXT



Like it? Please Spread the word!