Understanding loop equivalency
After having typed in both versions of each loop and run them, you may have begun to see some similarities between each of the looping statements. In fact, for counter-controlled looping, each of them is readily interchangeable.
To illustrate, let's examine each counter-controlled loop by comparing each of its essential parts.
The counter-controlled while()…
loop has the following syntax:
counter_initialization;
while( continuation_expression ) {
statement_body
counter_increment;
}
Note that both counter initialization and counter increments have been added to the basic syntax of the while()…
loop and that they are somewhat scattered about.
The counter-controlled for()…
loop has the following syntax:
for( counter_initialization ; continuation_expression ; counter_increment )
statement_body
It would be perfectly logical to assume that the for()…
...