Java : While loop

Posted in Conditional Loops, Java By Raj On November 18, 2017

 

The definition of while loop is something like below and the code which is inside the curly braces {}  executes until the conditional expression becomes false

Another example is if an external service or method is returning collections which you want to iterate and read the data and you don’t know how many elements are exist’s then you can use While loop

 

Java While Loop

Example

In the below code if the number is less than 10 then the code executes within the while block, when condition becomes false then it will come out of the loop

Output 

Example 2

Output