While Statements

while do

The while loop executes a block of codes as long as the specified condition is true.

Syntax

Run The Application

Right click to the WhileDemo class; select Run As -> Java Application.

Output

do while

The Java programming language also provides a do-while statement, which can be expressed as follows:

The difference between do-while and while is that do-while evaluates its expression at the bottom of the loop instead of the top. Therefore, the statements within the do block are always executed at least once, as shown in the following  program:

Run The Application

Right click to the DoWhileDemo class; select Run As -> Java Application.

Output