For Loops!!!

This is a discussion on For Loops!!! within the C++ Programming forums, part of the General Programming Boards category; 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(); ...

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    3

    Question 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?

  2. #2
    People Love Me
    Join Date
    Jan 2003
    Posts
    412

    Re: For Loops!!!

    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.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    x++;
    is the same as
    x + 1;

    You should read Tutorial : loops

  4. #4
    Registered User newbie_grg's Avatar
    Join Date
    Jul 2002
    Posts
    77

    ok..

    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;
    }
    "If knowledge can create problems, it is not through ignorance that we can solve them. "
    -Isaac Asimov(1920-1992)

  5. #5
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Cool 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!

  6. #6
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314
    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.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21