Syntax:
for(iVar1 = iX; iVar1 OP iVar2; iVar1
MATH)
{
Statement1;
Statement2;
....
}
Where,
iVar1 - The first variable to
compare
iX - The initial
value of iVar1
OP - The conditional
operator
iVar2 - The second variable to compare
MATH - A mathematical
operation performed on iVar1
Description:
Processes the statements within the brackets while
the conditional statement is true. The conditional statement first
sets the first variable to an initial value.
Every cycle, the first variable is compared to the
second variable. If this comparison is true, then the interpreter
breaks out of the for loop and continues processing the next step after
the closing bracket. If the comparison is not true, a math operation
is performed on the first variable. This operation is usually
used to change the variable so it is closer to causing the comparison
to yield a true value. If this is not the case, the flow control
may never exit and cause an infinite loop. Any of the variables
in the control statement can be changed in the middle of the loop.
Example
Code |
for(int
i = 0; i < 10; i++){
disp("The number is: " + string(i));
} |
Output |
The
number is: 0
The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5
The number is: 6
The number is: 7
The number is: 8
The number is: 9 |
See Also: while,
Conditional Statements, if-else