for
- Last UpdatedApr 24, 2024
- 1 minute read
The for statement executes its body while a specified Boolean logical operators evaluates to true. The for statement is composed of three ; separated elements ( ; ; ):
-
the initializer, which is executed only once before entering the loop and which usually is a local loop variable
-
the condition, which is a Boolean expression that determines if the next iteration in the loop should be executed, when evaluated to true
-
the iterator, that defines what happens after each execution of the body of the loop, usually a postfix increment.
Example code
static void testFor()
{
Runtime::Environment.Print("for started!");
int k2 = 0;
for (int kk = 0; (kk <12) && (k2 <= 0); kk++)
{
Runtime::Environment.Print("kk=" + kk);
If (kk == 10)
k2 = 1;// make the for finish when kk=10
}
Runtime::Environment.Print("for finished!");
}
//Output:
//for started!
//kk=0
//kk=1
//kk=2
//kk=3
//kk=4
//kk=5
//kk=6
//kk=7
//kk=8
//kk=9
//kk=10
//for finished!