|
Previous Next
for Loop
The "for" keyword is used to set up a loop to repeatedly execute the statements in its range until a certain condition is met. In the parentheses after the keyword "for", you define a starting value, an ending condition, and how to increment (or decrement) to reach the ending value. These are separated using ; . Use brackets { } when you want to execute more than one statement in the loop.
for (starting value; ending condition; increment)) {
execute this step 1;
execute this step 2;
execute this step 3;
}
An example might look like the following. The integer i will go from a value of 0 to 9. The integer k will go from a value of 0 to 45 in multiples of 5. The integer m will go from 0 to 63 in multiples of 7.
int k;
int m;
for (int i=0; i < 10; i++) {
k = 5 * i;
m = 7 * i;
}
Previous Next
|