Java – While loop

PREV     NEXT

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

Syntax:

while(condition)

{           //statements


}

Example:

COMPILE & RUN

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

Share this website !!!
Facebook Twitter Linkedin