IT Developer

Nested for Loops in Java

Chapter 10

Nested for Loops in Java

Class 10 - Logix Kips ICSE Computer Applications with BlueJ


Share with a Friend

Java Program: Java Output Programs


5 (i). What will be the value of sum after each of the following nested loops is executed?

int sum = 0;

for (int i = 0; i <= 10; i++)

    for (int j = 0; j <= 10; j++)

          sum += i;

5 (ii). What will be the value of sum after each of the following nested loops is executed?

int sum = 0;

for (int i = 1; i <= 3; i++)

    for (int j = 1; j <= 3; j++)

        sum = sum + (i + j);

 

Program Title 5 (i) : Value of sum After Nested Loops Execution in Java

int sum = 0;

for (int i = 0; i <= 10; i++)

    for (int j = 0; j <= 10; j++)

          sum += i;

Output

605

📝 Explanation

Step 1: Understand the loops

  • Outer loop: i runs from 0 to 10 → 11 values
  • Inner loop: j runs from 0 to 10 → 11 times for each i

👉 For each value of i, the statement sum += i executes 11 times.

Step 2: Calculate contribution of each i

For a fixed i, total added = 11 × i

So total sum:

11 × (0 + 1 + 2 + 3 + . . . + 10)

We know:

0 + 1 + 2 + . . . + 10 = 55

Therefore:

sum=11×55=605sum = 11 × 55 = 605sum=11×55=605

✅ Final Answer (5 i)

      sum = 605

Explanation with Dry Run

The outer loop executes 11 times. For each iteration of outer loop, the inner loop executes 11 times. For every value of j, i is added to sum 11 times. Consider the following table for the value of sum with each value of i and j.

i

j

Sum

Remarks

0

0 - 10

0

0 is added to sum 11 times

1

0 - 10

11

1 is added to sum 11 times
⇒ 0 + 11 = 11

2

0 - 10

33

2 is added to sum 11 times
⇒ 11 + 22 = 33

3

0 - 10

66

3 is added to sum 11 times
⇒ 33 + 33 = 66

4

0 - 10

110

4 is added to sum 11 times
⇒ 66 + 44 = 110

5

0 - 10

165

5 is added to sum 11 times
⇒ 110 + 55 = 165

6

0 - 10

231

6 is added to sum 11 times
⇒ 165 + 66 = 231

7

0 - 10

308

7 is added to sum 11 times
⇒ 231 + 77 = 308

8

0 - 10

396

8 is added to sum 11 times
⇒ 308 + 88 = 396

9

0 - 10

495

9 is added to sum 11 times
⇒ 396 + 99 = 495

10

0 - 10

605

10 is added to sum 11 times
⇒ 495 + 110 = 605

11

 

 

Loop terminates

Program Title 5(ii) : Value of sum After Nested Loops Execution in Java

int sum = 0;

for (int i = 1; i <= 3; i++)

    for (int j = 1; j <= 3; j++)

        sum = sum + (i + j);

 

Output

36

📝 Explanation

Step 1: Values of i and j

  • i = 1, 2, 3
  • j = 1, 2, 3

Total executions:

              3×3=9 times

Step 2: Expand all combinations

i j i + j
1 1 2
1 2 3
1 3 4
2 1 3
2 2 4
2 3 5
3 1 4
3 2 5
3 3 6

Now add all values:

               2 + 3 + 4 + 3 + 4 + 5 + 4 + 5 + 6 

Group properly:

              (2 + 3 + 4) + (3 + 4 + 5) + (4 + 5 + 6)   

               9 + 12 + 15 = 36

Final Answer (5 ii)

      sum=36

Explanation with Dry Run Table

The outer loop executes 3 times. For each iteration of outer loop, the inner loop executes 3 times. For every value of j, i + j is added to sum 3 times. Consider the following table for the value of sum with each value of i and j.

i

j

Sum

sum + (i + j)

1

1

2

0 + 1 + 1

 

2

5

2 + 1 + 2

 

3

9

5 + 1 + 3

2

1

12

9 + 2 + 1

 

2

16

12 + 2 + 2

 

3

21

16 + 2 + 3

3

1

25

21 + 3 + 1

 

2

30

25 + 3 + 2

 

3

36

30 + 3 + 3

4

 

 

Loop terminates