for loops

This is a discussion on for loops within the C++ Programming forums, part of the General Programming Boards category; i have a question i dont understand the difference in the syntax for the loop statement. here is what i ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    17

    for loops

    i have a question i dont understand the difference in the syntax for the loop statement. here is what i have:

    Code:
    int num = 7;
    int coun;
    
    for (coun=1; coun <=num; ++coun) <statement>;
    
    for (coun=1; coun <=num; ++coun); <statement>;
    i dont understand how the ; in the second for loop changes how many time the <statement> is run? what is the difference between how many times each statement will run? it is confusing me.

    thanks

  2. #2
    Registered User Boomba's Avatar
    Join Date
    Jun 2003
    Posts
    89
    it affect it because the statement is not under the loop anymore in the second part

    the ";" after the for loop in the second part ends whats under the looop early and makes it so that <statement> is not included in the loop

    therefore <statement> is run only once becasue it is not actually in the for loop.

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    for (coun=1; coun <=num; ++coun) <statement>;
    is the same as
    Code:
    for (coun=1; coun <=num; ++coun)
    {
      <statement>;
    }
    Code:
    for (coun=1; coun <=num; ++coun); <statement>;
    is the same as
    Code:
    for (coun=1; coun <=num; ++coun)
    {
    };
    
    <statement>;
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    The semicolon in the 2nd version of the code means that the loop ends right there. the statement after the semicolon is not being looped at all, it is just running as the next logical part of your program.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    17
    so in the 1st one the <statement> would be run 7 times and in the 2nd one it would only be run once because of the ;?

  6. #6
    Registered User Boomba's Avatar
    Join Date
    Jun 2003
    Posts
    89
    Quote Originally Posted by newbie101
    so in the 1st one the <statement> would be run 7 times and in the 2nd one it would only be run once because of the ;?
    Yes, correct

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote Originally Posted by newbie101
    so in the 1st one the <statement> would be run 7 times and in the 2nd one it would only be run once because of the ;?
    Yes, the statement does not even know of the loop's existence

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    17
    thank you now i get it. thanks again guys

  9. #9
    Registered User
    Join Date
    May 2006
    Posts
    17
    i actually have 2 more:

    Code:
    char Let = 'J';
    int Bo = -3;
    
    for (char Ind=Let; Ind<= 'J'; ++Ind) <statement>;
    
    for (int Boun=1; Boun>=Bo;  --Boun) <statement>;
    for the 1st one i dont think it will run at all since Ind=Let and Let is 'J' that makes Ind 'J' so in the next part where Ind<= 'J' he is already less the or = to 'J' since his is 'J' so the loop should run at all correct?

    the 2nd one the statement should run 4 times until Boun is >=Bo correct?

    thanks

  10. #10
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote Originally Posted by newbie101
    i actually have 2 more:

    Code:
    char Let = 'J';
    int Bo = -3;
    
    for (char Ind=Let; Ind<= 'J'; ++Ind) <statement>;
    
    for (int Boun=1; Boun>=Bo;  --Boun) <statement>;
    for the 1st one i dont think it will run at all since Ind=Let and Let is 'J' that makes Ind 'J' so in the next part where Ind<= 'J' he is already less the or = to 'J' since his is 'J' so the loop should run at all correct?

    the 2nd one the statement should run 4 times until Boun is >=Bo correct?

    thanks
    No, the 2nd expression in the for loop is the condition which must evaluate to true in order for the looped code to run. 'J'<='J' is true, therefore the statement is executed.

    after Ind is incremented, Ind becomes 'K' .. so 'K'<='J' is false.


    as for this one..
    Code:
    for (int Boun=1; Boun>=Bo;  --Boun) <statement>;
    look at the conditions.. remembering that >= means "Greater Than or Equal To"
    Code:
     1>=-3
     0>=-3
    -1>=-3
    -2>=-3
    -3>=-3
    Last edited by Bench82; 05-04-2006 at 10:32 AM.

  11. #11
    Registered User
    Join Date
    May 2006
    Posts
    17
    thanks now i understand

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