For loops are as easy as 1, 2, 3! Quite literally.
Unlike in some other languages where for loops always require at least one predefined variable, C++ for loops can be entirely independent!
Before we get into things, I must warn you. I'm not a fan of equals signs (=) by themselves. I think they're ugly, so when I define an integer I'll do so using the Python Syntax of parenthesis. These are still correct in C++ and I prefer them... Let's proceed!
Most common FOR loops :-
Probably the most common for loop you'll come across (and inevitably use) is a conditional loop of the following syntax:
for(int a(0); a <= Number_of_loops; a++)
{
//...
}
If this code were to be translated into English it would read something along the lines of: "I am a. I am equal to zero. I am going to execute the code "//..." and increase by one in value whilst I am still less than or equal to "Number_of_loops."
... More or less. Conditional loops still always execute their first and last loops.
You WILL use this loop many many many times. So here's a working example to cement the idea in your minds.
#include <iostream>
using namespace std;
for(int a(0); a <= 5; a++)
{
cout << a;
}
you can probably do this in your heads if I've explained it properly thus far. Just incase, the output should be "012345". We'll come back to this FOR loop later but for now we're moving on.
From here on in, if you understand the concept you can probably leave. Everything that we do with FOR loops is based on that first mathematical condition... FOR the most part.
FOR loops within FOR loops
Why would we put a FOR loop inside of a FOR loop??? To *scan* in another dimension of course, silly!
This is very important when we have lots of values in multiple dimensions and each dimension is significant! First off, let's get into what exactly putting a FOR loop inside of a FOR loop would do.
int D, C, I;
for(D(0); D <= 5; D++)
{
for(I(0); I <= 5; I++)
{
for(C(0); C <= 5; C++)
{
//...
}
}
}
D can't increase until everything inside of it has executed, likewise with I. Logically, C would have to go through its 5 loops before I could increase, then C would be reset to zero and loop all over again, and so on and so forth until I reaches 5. Only then can D increase.
When C has looped 125 times, I has looped 25 times, and D has looped 5 times, the loop ends.
That's 125 executions in 10 lines of code! Now THAT'S power! The power of looping through multiple dimensions! Add another dimension and you could have 625 conditional executions. Increase the number of loops from 5 to 50 and that's 6,250,000 executions in just 4 dimensions, that's no more than 13 lines of code!
Let's stop playing hypercube and get back to our tri-dimensional conditional statement; A conditional cube, if you will. Now imagine that we can put these FOR statements together to form X, Y, ans Z coordinates on that cube. If we wanted to find the position 0, 1, 2, we would have to go through 8 loops of C. Imagine traversing 3 units down the width of the cube and shooting up two units along the height to get to our Cartesian destination. We'll talk about what to do when we get there in the next segment.
FOR loops and Arrays
Now by now you may have already realized that arrays and FOR loops go together like peanut butter and jelly. DELICIOUSLY!
if we wanted to print all but 5 of the values in an array we could simply substitute our conditional integer into the array, like so.
for(int a(0); a <= (Size_of_Array-5); a++)
{
std::cout << array[a];
}
FOR loops give us a lot of control over arrays, and whilst I won't go too far into arrays I will bring you back to our conditional cube and tell you what to do there, if you haven't already found out.
FOR loops and Multidimensional Arrays
So we're on our cube of epic proportions and we've traversed the Cartesian planes of X, Y, and Z.
Let's put a multidimensional array in there! (Once again, I'm not going into these, you'll have to look them up yourself)
#include <iostream>
#define WIDTH 5
#define HEIGHT 5
#define DEPTH 5
using namespace std;
int multiarray[HEIGHT][WIDTH][DEPTH];
for(int D(0); D <= (HEIGHT-1); D++)
{
for(int I(0); I <= (WIDTH-1); I++)
{
for(int C(0); C <= (DEPTH-1); C++)
{
cout << multiarray[D][I][C];
}
}
}
This should roughly print every statement in that tri-dimensional array. By adding another FOR or IF statement we can check the actual value inside of that position and only print values equal to, for example, 5. By now you know that the possibilities are endless!
I use FOR statements to place tiles in a map for tile based games.
Spoiler
I even used them to help me deal with arrays so that I could create my very own virtual bidimensional array class.
They're an incredibly powerful tool in the programmer's arsenal, and I hope that by now you've learned to harness the awesome awesomeness that is the FOR statement.






MultiQuote








|