0

In the code below, I can trace how many times each of the for loops iterate if I look at them separately. For example, both the for loops iterate 10 times however, when you put them together, the String "hi" prints out more than 20 times.

How many times does the inner loop iterate?

for(int j=0; j<10; j++) 
    for(int k=10; k>0; k--)
        System.out.println("hi");
3
  • 6
    If you do ten things ten times, then you have done 100 things. Commented Jan 30, 2017 at 3:36
  • as @ElliottFrisch said, hi will be printed 100 times Commented Jan 30, 2017 at 3:38
  • Oh I see. So was I correct when I said that the inner loop iterates 10 times? Commented Jan 30, 2017 at 3:39

2 Answers 2

3

It is as simple as multiplying how many each iterate together (in this case 10*10). If you are finding it isn't as simple as this, you can perform the following test:

int count = 0;
for(int j=0; j<10; j++){
    for(int k=10; k>0; k--){
        count++;
    }
}
System.out.println("The nested loop iterated " + String.valueOf(count) + " times!");

Edit: Perhaps an easier way to understand what is going on:

int total_count = 0;
for(int j=0; j<10; j++){
    System.out.println("The outer loop has iterated " + String.valueOf(j+1) + " times!"); 
    System.out.println("Executing the inner loop");   
    int local_count = 0;
    for(int k=10; k>0; k--){
        local_count ++;
        total_count ++;
        System.out.println("Inner loop #" + String.valueOf(j+1) + " has iterated " + String.valueOf(local_count) + " times!");
        System.out.println("The inner loop's total iterations are " + String.valueOf(total_count) + " times!");
    }
}
System.out.println("The nested loop iterated " + String.valueOf(total_count) + " times!"); 
Sign up to request clarification or add additional context in comments.

6 Comments

This test print out that the nested loop iterates 100 times. Does that mean that it is not viewed separate from the other loop?
It is separate in the sense that each time the outer loop iterates the value of k is reset to 10, so the inner loop restarts. But because the outer loop iterates 10 times and the inner loop is INSIDE the outer loop, the inner loop is executed 10 times and iterates 10 times (10 x 10). So code within the inner loop will execute 100 times.
I think I am starting to understand. However, is asking "How many times does the inner loop iterate?" and "How many times does the nested loop iterate?" different? If they are different my understanding is that the inner loop iterates 10 times and the nested loop iterates 100 times. Please correct me if i am wrong.
You are right but it is important to know the nested loop is iterating 10 x 10 times. It is different from 100 times, as the value of k is reset to 10 each time. Very different from something like for (int i = 0; i < 100; i++){ // inside loop }
Sorry for so many edits, the second example should work now :)
|
0

When dealing with nested loops always try to visualize the first loop j as the row and the second loop k as the column.

enter image description here

The rows go left-to-right and the columns go top-to-bottom. If you multiply the row x column size then that is usually how many iterations you will encounter.

4 Comments

From this, my understanding is that the nested loop iterates 10 times but, the whole program iterates 100 times?
@ally The row (j in your case) iterates 10 times. Column (k in your case) iterates 10 times. Since you have nested the loops you are multiplying j x k, thus the nested loop iterates 100 times.
Did you really just bust out a multiplication table to answer a question? -_-
I think the multiplication table is fine, it gives ally a visual explanation of @coletrain answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.