A loop is a block of statements that is repeated.

A loop has a Boolean expression to test. If the expression is true the statements in the block are executed and then the test is performed again.
When the condition is false, the next statement after the loop is executed.

  • A while loop and a for loop test a Boolean expression at the beginning.
  • A do loop tests the Boolean expression at the end.
  • While and for loops are executed zero or more times.
  • Do loops are always executed at least once.

The Boolean expression is a check point: The decision is made to execute the entire loop or not. If the value of the Boolean expression changes during execution of the loop, the loop continues until the test is made again. The loop does not end in the middle if the variable changes.

NEXT: While Loops