For Loop

Discussion in 'Programming & Software Development' started by MrTanman, May 26, 2009.

  1. MrTanman

    MrTanman (Taking a Break)

    Joined:
    Nov 9, 2008
    Messages:
    100
    I'm currently in year 10 and am doing the Computer Skills Competition tomorrow. Last year in the test, I had to do programming questions, one of them is a For Loop. May someone please interpret the following question please. The answer is 20 but I cannot find out how.

    Code:
    t = 0;
    for (i = 1 to 3) {
              for (j = 0 to i) {
                        t = t + i;
                        next j;
              }
              next i;
    }
    display t;
    
    Help is greatly appreciated
     
  2. jaredoconnor

    jaredoconnor Member

    Joined:
    Mar 17, 2009
    Messages:
    79
    Location:
    Adelaide
    You should learn how to bench check in a grid. Since I have no grid, I'll use tabs and a really confusing way of showing it! :p

    Code:
    i is 1
    	j is 0
    		t = 0 + 1
    	j is 1
    		t = 1 + 1
    	j is 2
    		end inner loop
    
    i is 2
    	j is 0
    		t = 2 + 2
    	j is 1
    		t = 4 + 2
    	j is 2
    		t = 6 + 2
    	j is 3
    		end inner loop
    
    i is 3
    	j is 0
    		t = 8 + 3
    	j is 1
    		t = 11 + 3
    	j is 2
    		t = 14 + 3
    	j is 3
    		t = 17 + 3
    	j is 4
    		end inner loop
    
    i is 4
    	end outer loop
    
    Thus, t is 20.

    Alternatively...

    In the outer loop, i = 1 and has not yet gone above 3, so we start the inner loop.

    In the inner loop, j = 0 and has not gone above i, which is equal to 1. Thus, we perform the statements in the loop.
    t = t + i, which is t = 0 + 1.
    next j; increments j by 1. Now j = 1.

    In the inner loop, j = 1 and has not gone above i, which is equal to 1. Thus, we perform the statements in the loop.
    t = t + i, which is t = 1 + 1.
    next j; increments j by 1. Now j = 2.

    In the inner loop, j = 2 and HAS gone above i, which is equal to 1. Thus, THE INNER LOOP ENDS.

    Now we reach the next i; statement, which increments i by 1 and starts the OUTER LOOP again.

    In the outer loop, i = 2 and has not yet gone above 3, so we start the inner loop.

    In the inner loop, j = 0 and has not gone above i, which is NOW equal to 2. Thus, we perform the statements in the loop.
    t = t + i, which is t = 2 + 2.
    next j; increments j by 1. Now j = 1.

    etc.
     
    Last edited: May 26, 2009
  3. snax1104

    snax1104 Member

    Joined:
    Jan 12, 2009
    Messages:
    28
    Above is a very good explanation of how it works.
    I recommend coding it yourself tho as practice is always good.
    Add in a few display lines to explain what is going on.
    A simple C example would be something like this:

    Code:
    #include <stdio.h>
    
    int main (void){
      int t,i,j;
    
      t=0;
      for (i=1;i<=3;i++) {
        printf("loop i=%d\n",i);
        for (j=0;j<=i;j++) {
          t = t + i;
          printf("t=%d i=%d j=%d\n",t,i,j);
        }
        printf("\n");
      }
      printf("Result\nt=%d\n",t);
      return 0;
    }

    The output should be:
    loop i=1
    t=1 i=1 j=0
    t=2 i=1 j=1

    loop i=2
    t=4 i=2 j=0
    t=6 i=2 j=1
    t=8 i=2 j=2

    loop i=3
    t=11 i=3 j=0
    t=14 i=3 j=1
    t=17 i=3 j=2
    t=20 i=3 j=3

    Result
    t=20
     
  4. OP
    OP
    MrTanman

    MrTanman (Taking a Break)

    Joined:
    Nov 9, 2008
    Messages:
    100
    Awesome.:thumbup:

     

Share This Page

Advertisement: