8.8 The “for” Loop
You have seen in the while loop that you often initialize a counter variable for the loop and then increment it at the end of the loop. Because this is so common, it is packed together and thus simplified in the for loop:
for( Initialization ; Condition ; Update ) Statement
Statement and Initialization are statements, while Condition and Update are expressions.
It is common to define a loop variable in the initialization, modify it in the update, and check it in the condition.
A for loop corresponds to this well-organized while loop:
{ Initialization ; while( Condition ) { Statement ; Update ; } }
In Figure 8.5, you can see the process schematically.
I will rewrite Listing 8.12 into a for loop in the following listing. ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access