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.