0

I have put this code bu can't get the desired results.

    for (int i = 2; i <= 4; i+=2/* FINISH ME */) {
        for (int j =1; j < 5; j++){

        System.out.println("2 times " + j + " = " + (i*j));
        }
    }

And output was,

2 times 1 = 2
2 times 2 = 4
2 times 3 = 6
2 times 4 = 8
2 times 1 = 4
2 times 2 = 8
2 times 3 = 12
2 times 4 = 16

I don;t understand why the loop ran for 8 times and how the calculation gone so after four times?

Sunny Kk
  • 19
  • 1

3 Answers3

1

Here is what is going on in your loops: At the very beginning of your first loop i is set to 2, and then inner loop is starting and you get:

2 times 1 = 2 
2 times 2 = 4
2 times 3 = 6
2 times 4 = 8

Then first iteration comes to an end, so 2 is added to i (i+=2) and then it checks if i<=4 and since i==4 now in fact it is less or equal 4, so second iteration starts:

2 times 1 = 4 // i==4, so it is 4 times 1!!
2 times 2 = 8 //i==4, so it is 4 times 2!!
2 times 3 = 12
2 times 4 = 16
Vel
  • 524
  • 3
  • 11
0

In most cases, figuring out how many times a nested for loop will be run is just multiplication. You multiply the number of times the outer loop runs by the number of times the inner loop runs.

Here:

for (int i = 2; i <= 4; i+=2) {
    for (int j =1; j < 5; j++){

    System.out.println("2 times " + j + " = " + (i*j));
    }
}

The first loop runs for 2 times. The first time i is 2, the second time i is 4. There is no third time because i will then be 6 which is not less than or equal to 4.

The second loop is easier to figure out. You count up from 1 until it is not less than 5 anymore, so 1, 2, 3, 4, 4 times!

Now we can multiply 4 and 2 together to get 8, which is exactly how many times your System.out.println line is called.

Now, let's figure out the values of i and j. In a nested for loop, the variable in the inner loop changes at a faster rate than the one in the outer loop. It is only after the inner loop finishes, does the outer loop's variable change. So i starts off with 2, then j goes 1, 2, 3, 4 and the inner loop finishes. Now i is 4 and j goes 1, 2, 3, 4 again.

Now let's address the question of why the output says blatantly wrong things like 2 times 4 = 16. This is because of your print statement. You've made it so that it always starts with 2 times then j, then the value of i * j. So while i changes to 4 in the second iteration of the outer loop, you are still printing 2 times, when it should have been 4 times. To fix this, simply do:

System.out.println(i + " times " + j + " = " + (i*j));
Sweeper
  • 213,210
  • 22
  • 193
  • 313
0

I have made a little modification to your code. Hopefully, this will help you to understand your problem.

    int loopCount = 0;
    for (int i = 2; i <= 4; i += 2) {
        for (int j = 1; j < 5; j++) {
            System.out.println("loopCount = " + ++loopCount + " : i = " + i + ", j = " + j);
        }
        System.out.println();
    }

Output:

loopCount = 1 : i = 2, j = 1
loopCount = 2 : i = 2, j = 2
loopCount = 3 : i = 2, j = 3
loopCount = 4 : i = 2, j = 4

loopCount = 5 : i = 4, j = 1
loopCount = 6 : i = 4, j = 2
loopCount = 7 : i = 4, j = 3
loopCount = 8 : i = 4, j = 4
emon
  • 1,629
  • 1
  • 17
  • 18