Home Segments Top Top Previous Next

419: Mainline

The defect of many while loops is that the details that govern the looping appear in three places: the place where the counting variable is initialized, the place where it is tested, and the place where it is reassigned. Such distribution makes looping difficult to understand. Accordingly, you also need to know about the for statement:

for (entry expression; 
     Boolean expression; 
     continuation expression) 
  embedded statement or block 

The entry expression is evaluated only once, when the for statement is entered. Once the entry expression is evaluated, the Boolean expression is evaluated, and, if the result is true, the embedded statement or block is evaluated, followed by the continuation expression. Then, the test–evaluate loop continues until the Boolean expression eventually evaluates to false.