loading

Do/While Loop

The Do/While Loop

One variation of the while loop is the do/while loop. This loop will run the code block once, then check to see if the condition is true before repeating the loop indefinitely.

Syntax

				
					do {
  // code block to be executed
}
while (condition);
				
			

A do/while loop is used in the example below. Because the code block runs before the condition is tested, the loop will always run at least once, even if the condition is false:

Example

				
					int i = 0;
do {
  System.out.println(i);
  i++;
}
while (i < 5);
				
			

The loop will never stop if you don’t increase the variable used in the condition!

Share this Doc

Do/While Loop

Or copy link

Explore Topic