Thread: Help me understand For Looping

  1. #1
    Unregistered
    Guest

    Help me understand For Looping

    Someone please help me understand for looping. I don't get it. I'm so lost.

  2. #2
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    Example:

    int i;

    for(i = 0; i < 10;i++)
    {
    //stuff
    }

    Program execution gets to the loop, and begins to execute the loop. The first part (i = 0) is initialization of the loop variable (the var that controls how many times the loop is executed). The second part is the condition - the loop will coninue to execute as long as this true. Our loop will execute as long as i's value is less than 10. The last part (i++) is incerementing the loop variable. This allows a way to eventually exit the loop (since incrementing i repeatedly will eventually get it to 10)

    1st pass:
    i=0, i<10, go into loop
    2md pass:
    i=1, i<10, go into loop
    3rd pass:
    i=2, i<10, go into loop
    4th pass:
    i=3, i<10, go into loop
    ...
    Until
    10th pass:
    i = 10, i is not <10, skip over loop and continue execution.

  3. #3
    Unregistered
    Guest
    Thanks!

Popular pages Recent additions subscribe to a feed