The for Statement
Besides a condition and a body, lots of loops require an initialization phase as a preamble to the execution of the loop. In addition to this, it’s common to have an update phase that gets executed at the end of each iteration. The for statement is what enables cleaner syntax for this scenario:
for (initializer; condition; iterator) embedded-statement
As usual, the embedded statement has an optional pair of curly braces surrounding it. (The usual remarks apply.)
The for statement can be seen as shorthand syntax for the following equivalent while statement:
{ initializer; while (condition) { embedded-statement; iterator; }}
Notice the outer pair of curly braces establishing a scope for variables that ...
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