< Previous | Contents | Next >

Using do Loops

Like while loops, do loops let you repeat a section of code based on an expression. The difference is that a do loop tests its expression after each loop iteration. This means that the loop body is always executed at least once. Heres a generic form of a do loop:

do

statement; while (expression);

The program executes statement and then, as long as expression tests true, the loop repeats. Once expression tests false, the loop ends.


 

Introducing the Play Again 2.0 ProgramLooping with a do Loop