C++: For Loops
In the previous tutorial when we were learning about arrays, I alluded to something called the for loop. It's essentially just another kind of loop, much like while, which makes creating a loop which should loop a certain number of times really simple.
Perhaps one of the best ways to show off the for loop is to simply cycle through and output all the elements in an array. So first, let's just create an array that we can cycle through inside of our main function – I'm going to use an array of strings (make sure to #include <string>!):
string Passwords[5] = { "password", "abc123", "root", "admin", "awesome" };
Now we have the array, let's start creating the loop itself. The for loop is created by using the for keyword, and then specifying three different sections, separated by semicolons, inside brackets. Code to execute each loop is then put inside curly brackets that follow this. The first section the for loop takes is called the declaration, and should contain the declaration (and probably initialization) of an iterator variable that the loop will use. The second section is called the condition - this should contain the condition which must remain true for the loop to keep going. The third and final section, is called the update - this should contain some code which updates the iterator variable (for example, incrementation). The general idea should look something like the following:
for(declaration; condition; update)
{
//Code to loop in here
}
All we have to do now is populate the sections. Something like the following will do fine to loop round 10 times (with i going from 0 to 9):
for(int i = 0; i < 10; i++)
{
//Code to loop in here
}
It's worth noting while we're talking about the sections of these loops, that not all sections are necessary. If you wanted an infinite loop that uses for for example, you could leave all the sections empty and simply write:
for(;;)
{
//Code to loop in here
}
Getting back to our purpose, we want to execute the code inside as many times as our array has elements. We could hard-wire it in and have it loop round exactly five times, but this isn't the most elegant solution as we may wish to change the number of elements in the array at a later date, at which point we would have to update all the code (which isn't what we want). A better solution might be to get the exact number of elements in the array, but unfortunately there isn't a nice method to do that -- we can however count the number of elements if we get a bit creative.
There are a number of ways to go about doing this, but by far the easiest way to do it in our case is to divide the total size of our array (in bytes) by the space each element takes up (in bytes). There is a function that can get the size of stuff (in bytes), and that function is called sizeof. With all of this in mind, the calculation to get the number of elements should look like the following:
sizeof(Passwords) / sizeof(Passwords[0])
Note that although we are dividing by the sizeof the first element in the array above, the result would be the same if we divided by sizeof(string) -- using the first element in the array is just slightly more flexible as we may wish to change the array datatype in the future (although hopefully not to anything too complex as this method of counting the elements doesn't work with everything).
So now we can count the elements in the array, writing out the for loop shouldn't be too difficult. We want to create an iterator variable, which we'll call i, loop while it is less that the number of elements (the calculation we just worked out), and then increment i each time. The result should look something like the following:
for(int i = 0; i < (sizeof(Passwords) / sizeof(Passwords[0])); i++)
{
//Code to loop in here
}
From here, all we need to do is add the cout that outputs the array element with the index of i - and we've finished our program! The full source code, which includes some extra little pieces of cout polish, is as follows:
#include <iostream>
#include <conio.h>
#include <string>
using namespace std;
int main()
{
string Passwords[5] = { "password", "abc123", "root", "admin", "awesome" };
cout << "The passwords are as follows:\n" << endl;
for(int i = 0; i < (sizeof(Passwords) / sizeof(Passwords[0])); i++)
{
cout << "Password " << i+1 << ": " << Passwords[i] << endl;
}
getch();
return 0;
}
Back to C++

