Now that we know how to create and mess with arrays, we'll look at a control statement that makes doing so a great deal quicker and easier. The for loop is a structure that repeatedly executes a piece of code until certain conditions are met. Aside from the template which always stays the same, there are 4 parts to the for loop. The first is any variable declarations that you want to execute at the beginning of the loop (they'll disappear after the loop is done). Part 2 is the condition under which the loop will stop executing -- you'll remember these from lesson 4 on if statements. Part 3 houses any modifications you want to make to any variables after each run of the loop -- most use a variable that was used in the condition (part 2), but that is not a requirement. Lastly, there is the code to execute. All said, here is what a for loop looks like.
for(part 1; part 2; part 3)
{
part 4
}
You'll note the semicolons in between each part. You aren't required to put something in any of the parts; the semicolons act as delimiters should you wish to omit a part. Remember, though, that omitting the second part (the condition) will make your loop run forever!
The important part, though, is how the loop functions. First, it executes part 1: the variable declarations. Then it checks part 2, the condition. If the condition evaluates to true, it will execute the code contained in part 4. Once that is done, it will execute part 3, the modifications, and then back to part 2, then to 4, then 3, and so on in that fashion until part 2 evaluates to false, at which point it will leave the loop. Here is a full example using an array.
int * a = (int*)malloc(sizeof(int) * 5);
for(int z = 0; z < 5; z++)
{
*(a + z) = z + 1;
cout << a[z] << endl;
}
Is your head spinning yet? Don't worry, I'll explain it all. First off, you should remember the array declaration from last lesson. Next, you'll see I replaced part 1 with another variable declaration, int z = 0. This creates a variable that only exists within the for loop. Next is the condition, z < 5. This means that the code will execute while z is less than 5, which is technically until z is >= 5. Next we have part 3, z++. This will add 1 to z after each run-through, or iteration, of the loop. Notice how I'm modifying the variable I used in my condition (which I, in turn, declared in the declaration phase) here. It isn't required, but you might as well do it. Lastly, we have the code contained in the curly brackets. In this example, I simply visit each element of the array a, stick a number in it, and print that number out. The loop will run 5 times; once with z = 0, z = 1, z = 2, z = 3, and z = 4. After that, it'll bump z up to 5, which will make the condition evaluate to false.
For loops are almost exclusively used in conjunction with arrays. In fact, what we just did above is used so often (in one form or another) that it has its own name: traversing the array. Oh, and by the way, you are also permitted to place multiple declarations/modifications within your for loop. Simply delimit each one with a comma. However, all of your declarations must share the same data type; repeating the data type will result in a syntax error. Here's a brief example showing multiple declarations.
for(int a = 0, b = 5; b > 0; a++, b--)
There is no change in functionality here: it will just be modifying multiple variables and checking multiple conditions instead of just one.
On a side note, you CAN place multiple comma delimited conditions within a for loop without your program throwing a syntax error at you; however, it will only consider the rightmost condition, discarding the rest of them. Take this code:
for(int a = 10, b = 5;a < 5, b > 0; a++, b--)
Pay special attention to the initialization of a to 10 and the condition, a < 5. Alone, these would cause the for loop to exit without executing it's code. However, since there is another condition to the right of a < 5, the for loop executes until THAT one becomes false. Thanks, Ying, for the clarification on that one!