The for Loop

The for loop combines three parts—initialization, condition, and step—into one statement at the top of the block it controls. A for statement has the following form:

for (initialization;
					condition;
					step)
{
   statements;
} ;

The first part of the for statement is the initialization. Any legal C++ statements can be put here (separated by commas), but typically this is used to create and initialize an index variable. Such a variable is most commonly an int. This part ends with a semicolon and is only performed once before the loop starts.

Next is the condition, which can be any legal C++ bool expression. This serves the same role as the condition in the while loop and should be read as “while Index is less than myNumberOfEntries” in the example. This part also ends with a semicolon, but it is performed at the start of every repetition.

Finally there is the step. Typically, the index variable is incremented or decremented here, although any legal C++ statements, separated by commas, are valid. This part is performed at the end of every repetition.

Note that the index of the loop, or any other variable declared in the initialization part of the for statement, can be used inside the loop, but not outside it. In the case of the loop at line 28 in Tape(), the variable Index cannot be used at line 32, but can be used at line 30.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.247.166