C Plus Plus Loop
From GMpedia.org Wiki
Contents |
[edit] The Loops
[edit] while
The while loop executes a statement over and over as long as the condition written in the while statement itself is ture.
while(<statement>)
{
<expression>
}
[edit] do-while
The function of the do-while loop is identical to that of the while loop, the only difference between them is in the syntax.
do
{
<expression>
}
while(<statement>)
[edit] for
The for statement has the following syntax: for (<initialization>; <while-condition>; <between-loops expression>) The <initialization> expression is executed once before the loop, it usually defines the variable that will be used as a counter. For example:
i=0
The <while-condition> is a condition (statement) that will be evaluated each time. If the statement is true, the loop is repeated, else the loop stops, and the rest of the code is executed. The while-condition usually performs a logical operation to evaluate the value of the counter. An example of the while-condition is:
i<10
The <between-loops expression> is an expression that is executed each time before the <while-condition> is evaluated. The expression usually increments a number to the counter, so that when the while-condition is evaluated, it will return false after a certain number of loop-repititions. An example on that is:
i++
A full example on the code will be:
for(i=0;i<10;i++)
{
<perform expression>
}
The example above repeates the expression 10 times, because it:
- sets i to 0
- increments 1 to i before each loop
- evaluates if i is still less than 10 before each loop
IF (i) is still less than 10, the loop is again repeated. ELSE the loop stops.

