For loop

This is a discussion on For loop within the C Programming forums, part of the General Programming Boards category; Hi! I have a question about for loops. I have this: Code: for (i=0; i<5; i++) { for (k=0; k<4; ...

  1. #1
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669

    Question For loop

    Hi!

    I have a question about for loops.

    I have this:

    Code:
    for (i=0; i<5; i++) {
    for (k=0; k<4; k++) {
    for (l=0; l<6; l++) }
    /* some code */
    }
    }
    }
    Is it important the order in the for loops?
    Must be the value of the first loop the biggest or the smallest (i<5)?
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  2. #2
    B26354 Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    If there are no other instructions in the first and second for statements, the order is not important. In your example, 'some code' is being executed 120 times.

    You could simplify this with:

    Code:
    for ( i=0; i<120; i++ )
    {
      /* some code */
    }
    However, if there is more code to be handled within these for loops (but outside the third loop), then the order is important.

    (I'm assuming the brace after the third for loop is intended to be { and not } ).
    Last edited by Deckard; 01-05-2002 at 11:18 AM.

  3. #3
    Unregistered
    Guest
    >Is it important the order in the for loops?
    Yes, it's very important. The inner loops must need to be performed in their entirety for each iteration of the loop immediately outside it. So if I needed three loops, A loop must run 4 times, B loop must run three times with each iteration of A loop, and C loop must run 6 times inside B loop I would write this code.
    Code:
    for(A = 0; A < 4; ++A){
      for(B = 0; B < 3; ++B){
        for(C = 0; C < 6; ++C){
            /*do stuff*/
        }
      }
    }

  4. #4
    B26354 Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633

    Re: For loop

    Originally posted by GaPe
    Must be the value of the first loop the biggest or the smallest (i<5)?
    Oops, forgot to answer all of your question

    The answer is neither. As long as the expression results in a true condition, the code within the for statement will be executed.

    The for statement has four main parts:

    Code:
    for ( initilization; test; increment )
    {
      additional code/statements
    }
    When the for statement is first encountered in your program, the initilization expression is executed (i=0). Then we check the expression in test to see if it evaluates true (i<5). Since we just initialized i to zero, i is certainly less than five, so the body of the for statement is executed (in your case, another for statement). After the code in the body completes, the expression in increment is executed (i++). The loop continues as long as test equates to true.

    I hope this explination is useful to you in future implementation of the for statement.
    Jason Deckard

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