- Home
- Chapter 1 - Object Oriented Programming Concepts
- Object Oriented Programming Concepts
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions
- Chapter 2 - Introduction to Java
- Introduction to Java
- Multiple Choice Questions
- Assignment Questions
- Chapter 3 - Values and Data Types
- Values and Data Types
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions
- Chapter 4 - Operators in Java
- Operators in Java
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions
- Chapter 5 - User-Defined Methods
- User-Defined Methods
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions
- Chapter 6 - Input in Java
- Input in Java
- Multiple Choice Questions
- Assignment Questions and Programs
- Chapter 7 - Mathematical Library Methods
- Mathematical Library Methods
- Multiple Choice Questions
- Assignment Questions
- Chapter 8 - Conditional Constructs in Java
- Conditional Constructs in Java
- Multiple Choice Questions
- Assignment Questions and Programs
- Chapter 9 - Iterative Constructs in Java
- Iterative Constructs in Java
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions and Programs
- Chapter 10 - Nested for loops
- Nested for loops
- Assignment Questions and Programs
- Chapter 11 - Constructors
- Constructors
- Multiple Choice Questions
- Assignment Questions and Programs
- Chapter 12 - Library Classes
- Library Classes
- Multiple Choice Questions
- Assignment Questions
- Chapter 13 - Encapsulation and Inheritance
- Library Classes
- Multiple Choice Questions
- Assignment Questions
- Chapter 14 - Arrays
- Library Classes
- Multiple Choice Questions
- Assignment Questions
- Chapter 15 - String Handling
- Library Classes
- Multiple Choice Questions
- Assignment Questions
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 |
|
2 |
0 - 10 |
33 |
2 is added to sum 11 times |
|
3 |
0 - 10 |
66 |
3 is added to sum 11 times |
|
4 |
0 - 10 |
110 |
4 is added to sum 11 times |
|
5 |
0 - 10 |
165 |
5 is added to sum 11 times |
|
6 |
0 - 10 |
231 |
6 is added to sum 11 times |
|
7 |
0 - 10 |
308 |
7 is added to sum 11 times |
|
8 |
0 - 10 |
396 |
8 is added to sum 11 times |
|
9 |
0 - 10 |
495 |
9 is added to sum 11 times |
|
10 |
0 - 10 |
605 |
10 is added to sum 11 times |
|
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 |
