for loop

This is a discussion on for loop within the C Programming forums, part of the General Programming Boards category; Hi Below is a small piece of code posted to the board a few days ago, can some one explain ...

  1. #1
    Unregistered
    Guest

    Question for loop

    Hi
    Below is a small piece of code posted to the board a few days ago, can some one explain to me what is happening to it??
    I realise that it is increaseing by three but what makes 'i' print three idetical numbers but 'j' prints numbers sequentialy.
    Are the two for's not the same??

    #include <stdio.h>

    int main(void)
    {

    int i, j ;

    for (i = 1; i <= 3; i++)
    for (j = 1; j <= 3; j++)
    printf("%d ------ %d\n", i, j);
    system("pause");
    return 0;
    }

    the way the numbers are displayed seems to be controled by the 'i' for !!. If I alter the numbers in the for's 'i' I get
    two diffrent displays but governed by what is in the 'i' for.

    #include <stdio.h>

    int main(void)
    {

    int i, j ;

    for (i = 1; i <= 3; i++)
    for (j = 1; j <= 6; j++)
    printf("%d ------ %d\n", i, j);
    system("pause");
    return 0;
    }

    the code above gives :-
    1 1
    1 2
    1 3
    1 4
    1 5
    1 6
    2 1
    2 2ect,ect up to a count of three on the left. But six digits on the right.why does it not display six out puts on the right.If I put six in the 'i' for it displays six units(left and right)

  2. #2
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,230
    Ok, you obviously need a bit of help with loops...

    What's happening is that first, your first loop goes into action, which activates the second loop, which is set to increment 6 times (so you get 6 values) then it returns to the topmost loop, which repeats the process 2 more times (it's set to do it 3 times, so you get 3 values) you get the value of 1 six times because the top loop won't spin (increment, whatever) again until the bottom loop is finished, which isn't until it loops 6 times. Get it?

  3. #3
    Unregistered
    Guest
    Hi
    Right, did not realise the second 'for' was the statment to the first 'for'. But......................what tells printf to pad out the first 'for'
    why do you not get the following display
    1 1
    2
    3
    4
    5
    6
    2 1
    2
    3
    4
    5
    6
    3 1
    2
    3
    4
    5
    6
    please fore give my ignorance
    I must be doing some thing wrong as the board will not display the numbers right, I hope you under stand what I mean

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    If you indent and add blocks it makes a little more sense.
    Code:
    #include <stdio.h> 
    
    int main(void) 
    {
      int i, j ; 
    
      for (i = 1; i <= 3; i++) //exterior loop
      { 
        for (j = 1; j <= 3; j++) //interior loop
        {
          printf("%d ------ %d\n", i, j);  //first print is 1------1
        }
      }
      return 0; 
    }
    What is happening is that when you finally call your print function, you are already inside the interior loop, this means that the value for i will not change until you meet the condition to exit the interior loop (ie. when j == 3). When you exit the interior loop, the exterior loop will increment by one and then you enter the interior loop again. The same thing happens.

    So i will stay the same while j is being incremented, and will only be incremented itself when j is done with it's loop. So your output is:
    1--------1
    1--------2
    1--------3
    2--------1
    2--------2
    2--------3
    3--------1
    3--------2
    3--------3
    //done
    -Prelude

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    A classic example of "nested loops".
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    If you wanted to run the loops independantly, you must do:
    PHP Code:
    for(i=0;i<3;i++)
     { 
      
    printf("%i \n",i);  
     }
    for(
    j=0;j<3;j++)
     {
      
    printf("%i \n",j);
     } 
    Otherwise, (as in the code you posted) the compiler thinks you meant:

    PHP Code:
    for(i=0;i<3;i++)
     { 
       for(
    j=0;j<3;j++)
        {
         
    printf("%i  %i\n",i,j);
        }
     } 
    So, no, they cannot share the same printf() statement if you want them to run separately...
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

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