O'Reilly logo

C# 5.0 Unleashed by Bart De Smet

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required