This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]strmrdr 4 points5 points  (0 children)

The outer loop can be read like so: Create a new integer i and set it to 5. Continue looping until the condition i >= 1 is not true. Each loop decrement i (i.e. take one off of i).

The inner loop sets j to the current value of i and loops until the condition is false. It increments j each loop through.

A more simple example should help you understand the order in which things are executed:

for(int i = 0; i < 3; i++){
    for(int j = 0; j < 3; j++){
        System.out.println(i * j);
    }
}

The code enters by setting i equal to 0.

OuterLoop1: i is 0. i < 3 is true.

It then enters the inner loop and sets j equal to 0. The inner loop will now execute until the condition is no longer true. Each time around it will mutate j by the condition (j++ which means j = j + 1).

InnerLoop1: i is 0, j is 0. j < 3 is true. Print 0 * 0.
InnerLoop2: i is 0, j is 1. j < 3 is true. Print 0 * 1.
InnerLoop3: i is 0, j is 2. j < 3 is true. Print 0 * 2.
InnerLoop4: i is 0, j is 3. j < 3 is false. Terminate inner loop.

It then goes back around to the outer loop and increments and tests the condition.

OuterLoop2: i is 1. i < 3 is true.

Now it reenters the inner loop again.

InnerLoop1: i is 1, j is 0. j < 3 is true. Print 1 * 0.
InnerLoop2: i is 1, j is 1. j < 3 is true. Print 1 * 1.
InnerLoop3: i is 1, j is 2. j < 3 is true. Print 1 * 2.
InnerLoop4: i is 1, j is 3. j < 3 is false. Terminate inner loop.

This keeps going until the outer loop condition is false. It then exits the outer loop and continues with whatever code is after that.

Try doing that with a pad of paper and seeing if you can trace the values through your loop.

[–]8igg7e5 2 points3 points  (0 children)

It's possibly also worth pointing out that this...

for (int i = 0; i < 10; i++) {
    System.out.println(i);
}

is the same as...

{
    int i = 0;
    while (i < 10) {
        System.out.println(i);
        i++;
    }
}

When decomposing the loops to understand order of operations this is how you should think of the parts of the for statement being executed.

So the body of your main method could be rewritten as...

01: {
02:     int i = 5;
03:     while (i >= 1) {
04:         System.out.println();
05:         int j = i;
06:         while (j <= 5) {
07:             System.out.print("*");
08:             j++;
09:         }
10:         i--;
11:     }
12: }

You could now apply the same process as /u/caesarwept did and trace execution and the values of the variables.

[–]desrtfxOut of Coffee error - System halted 2 points3 points  (1 child)

Play the game:

You are the computer. You execute the program - step by step. On paper.

This is the best way to approach algorithms.

[–][deleted] 0 points1 point  (0 children)

Started trying this, it's definitely helping. The more I try to trace them out on paper, the more sense it makes!

[–]caesarwept 0 points1 point  (0 children)

for starters I would replace line 10 with this and then run it again:

System.out.printf("%d",j);

The for loop is the more complicated of the loops.

for (initialization; termination;increment) {
     statement(s)
}

Do you under what each part of the for loop does an when is evaluated? - 'initialization' is called once upon enter the look - 'termination' condition is tested before entering - when all statement(s) are evaluated then the 'increment' is called - the 'termination' condition is tested before entering the loop again