# For Loops!!!

• 08-14-2003
Nailogamer
For Loops!!!
I dont understand for loops, like this one:

Code:

```#include <iostream.h>      int main()                      {        for(int x=0;x<100;x++) {                                                                  cout<<x<<endl;          } std::cin.get(); return 0; }```

How can I make it go up by 5, or by 10 till it gets to 100, how does the x++ work? I get x=0 and x<100, but not x++... can someone explain for loops to me?
• 08-14-2003
Krak
Re: For Loops!!!
Quote:

Originally posted by Nailogamer
I dont understand for loops, like this one:

Code:

```#include <iostream.h>      int main()                      {        for(int x=0;x<100;x++) {                                                                  cout<<x<<endl;          } std::cin.get(); return 0; }```

-How can I make it go up by 5, or by 10 till it gets to 100?
----------
-how does the x++ work?

-for(int x=0;x<100;x+=10)
----------
- ++ is an increment operator, which means it adds the number by one.

So if you had the following code,

int x=5;
x++;

x would be 6.
• 08-14-2003
laasunde
x++;
is the same as
x + 1;

You should read Tutorial : loops
• 08-14-2003
newbie_grg
ok..
Quote:

Nailogamer wrote: For Loops!!!
For Loops!!!
I dont understand for loops, like this one:
Code:

``` code:-------------------------------------------------------------------------------- #include <iostream.h>      int main()                      {        for(int x=0;x<100;x++) {                                                                  cout<<x<<endl;          } std::cin.get(); return 0; } -------------------------------------------------------------------------------- How can I make it go up by 5, or by 10 till it gets to 100, how does the x++ work? I get x=0 and x<100, but not x++... can someone explain for loops to me?```

This should work fine
Code:

```#include <iostream> // iostream.h will also work int main()                      {        for(int x=0;x<100;x+=5)  // increment each value of x by 5;  { cout<<x<<endl;          } std::cin.get(); return 0; }  //Or even if you dont get this then have look at this int main()                      {        for(int x=0;x<100;)  //  No increment here { x = x + 5;              //increment each value of x by 5; cout<<x<<endl;          } std::cin.get(); return 0; }```
• 08-14-2003
DougDbug
Loops are beautiful, man !
Take it slow, and make sure you understand loops... for-loops, do-loops, and do-while-loops.

Because, loops (doing stuff over-and-over) and decision making ("branching" with if and switch/case statements) are the two basic concepts that make all programming worthwhile!
• 08-14-2003
darksaidin
Quote:

Originally posted by laasunde
x++;
is the same as
x + 1;

You should read Tutorial : loops

No, imho x++ equals x=x+1.

Code:

`for(int x=0;x<100;x++)`
int x=0 creates and initializes an integer variable with the value 0.

After the first semicolon, x<100 is the condition for the loop. The loop will end if this condition returns false (i.e. assume "x" is 101 then the condition would be 101<100 which is not true and would cause "for" to exit the loop)

After the second semicolon, x++ means that x will be incremented by one for every time the the code in the loop has been executed.

Notice that the order is important.