| Home | Links |

Do While Loops

  • Similar to while loops – difference is that for Do While loops, the condition is checked after the loop executes
  • Post-test loops
  • Loop body is executed at least once
  • Used when the number of execution times is not predetermined

General Formula

do
{

Statement(s) or block;

}
while (Boolean test);

Example

int x = 0;
//initialization
do
{

System.out.print (x + “ “);
x ++;
// testing value of variable “x”

}
While (x < 10);
// when x = 10, loop will stop repeating above statement and then statement below will appear
System.out.println (“Done”);