Thursday, November 5, 2015

The "For" Loop

//In this post, I'm referring to the for loop as it functions in C++. It might function differently in other //languages, so I can't touch up on those.
For loops are beautiful yet complicated things. If you understand it, the for loop can be your best friend. If you don't, it'll be your worst nightmare. I want to help as many people as possible to understand it, because once you do, THE WORLD OF C++ WILL BE YOURS TO CONTROL.
//Definitely not exaggeration
To start, its important to note that the for loop is, well, a loop. It will keep going as long as the requirements are met. What differs this from a while loop is the ways you can word your requirements. Here's an example of a for loop.

for(int i=7, i>10, i++)
{
cout<<i<<"This is the value of i.";
}
Now what does all of this do? Here's a breakdown:

The "for" at the beginning registered it as a "for" loop.
Everything inside the parentheses is a requirement for the for loop to run, or setting up an escape route. The loop will run as long as the requirements are met, but stop when when the requirements stop being met.
The "int" is stated the creation of an integer. If you have a integer you want to use in the for loop requirements, you can simply put it where the "i" is and not write "int".
The "i=7" is stated what the integer is called (i), and what the integer is equal to (7).
The "i>10"  is stating the limitations. In this case, the loop will continue until the integer "i" equals at least 10, when it will stop.
The "i++" is stating that at the end of one cycle to add 1 to the integer "i".
Everything inside the {} is what is done during to loop.

Now, what can for loops be used for? For loops can be used for many things, but I find the most useful way to use it is for simple math programs. Because with it, you can create programs to solve the most annoying math problems.
And who doesn't want that?

Romain, stop purifying the dwarves!
-GIR