Could someone help explain For loops to me?

I have looked up the definition of a for loop on the internet, but I just got more confused, could someone help explain for loops to me with examples of how they work?

Thank You, RGB 455
Any loop will execute while a condition is met. For example lets say I have an array of 10 elements, you could set up a loop that displays all elements of the array by crating a for loop that executes 10 times, so..
1
2
3
4
for(int i = 0; i<10;i++)
{
cout<<array[i];
}

in the above code i is set to 0 and incremented until it reaches 10, while i is not equal to 10 the loop will execute, this means that it will do so 10 times, exactly the number of times you want it to.

on the other hand in comparison a while loop would work by saying, while the array is not empty, execute.
So a for loop will add, subtract, or do a form of math I tell it to do, until it reaches a certain number?


for (x= 4)

could this work? It says I need to declare x, is x the number it needs to reach?
http://www.cplusplus.com/doc/tutorial/control/


for (initialization; condition; increase) statement;

The for loop has three major components. Initialization sets the starting value for the variable you're working on. Condition is how the loop knows when to stop, and increase is any change made to the variable, preferably to move it closer to the condition.

Your example of
for (x=4)
is invalid.

It may work as for (x=4;;). However, in this case, x starts as 4, and does not change (although you may change it inside of the loop), and has no end condition, which means you are required to break inside of the loop or it will go on infinitely.

Example :
1
2
3
4
5
int x;
for (x=4;;) {
    if (x==5) break;
    ++x;
}

This is a simple example where everything would be better suited to fit in the original line, but it illustrates the idea.

It may also work as It may work as for (;x=4;). In this case, x has a predetermined starting value, and will need to be changed inside of the loop. The loop will stop when x = 4.

Example:
1
2
3
4
int x=3;
for (;x=4;) {
++x;
}

This is a simple example where everything would be better suited to fit in the original line, but it illustrates the idea.
Don't waste your time looking up definitions of loops, spend the time to read a tutorial on control structures so you understand WHY you use loops in programming. It always becomes more clear if you apply new information as you learn it. I would start with the page here at Cplusplus.com: http://www.cplusplus.com/doc/tutorial/control/
So a for loop will add, subtract, or do a form of math I tell it to do, until it reaches a certain number?

Yep

By declaring x, it means that your variable 'x' does not exist. You could fix it by setting it up like this.

for (int x = 4)


You have fixed it by "declaring" x (basically meaning you let the computer know that x is an integer). So now what? Well, I'm afraid your code won't do anything. All you've done is stated the initial value of the for loop. In other words, is is not the number the computer needs to reach, rather it is the number the computer starts at.

for (int x = 4; x < 14)

I've set it so 14 is the number my computer needs to reach.

Just a reminder, there are 3 parts of a for loop, 1.) A value to start at, 2.) a value to reach, 3.) a mathematical operation (such as adding) to reach the final value. We already did the first two. So we just need to include a mathematical operation.

for (int x = 4; x < 14; x++)
But what if I don't like x++? Well, there are other tools as well.

So let's see how this looks altogether.
1
2
3
for (int x = 0; x < 10; x++) {
      cout << "hello world\n";
}

Note that I changed the start and end values from 4 and 14 to 0 and 10. Now I'm going to show you what the output is

hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world


See how it displays hello world 10 times? That's because we keeped doing x++ until x < 10 was no longer a true statement.
Try reading this:
http://www.programming4beginners.com/tutorial/chapter05/more-about-for-loops
(and the few pages that follow)

And then this:
http://www.programming4beginners.com/tutorial/chapter12/for-loops-in-detail

Also, before understanding for loops, you should understand that statements in a program get executed in a specific order, one by one. YOu must understand this before you attempt to understand for loops.
Last edited on
Thank you guys!
Topic archived. No new replies allowed.