Skip to content
Computer ApplicationsICSE Class 9 · Nested for Loops

Series Programs and break and continue Inside Nested Loops in Java (ICSE Class 9)

Updated 18 October 2026 · Fact-checked

Series whose terms are themselves sums or products need a nested loop: the inner loop builds one term and the outer loop adds it to the total. A break or continue inside the inner loop affects only that inner loop; the outer loop carries on. The classic uses are the series 1 + (1+2) + (1+2+3) + ..., the series of factorials and a prime check that stops at the first divisor.

The red break arrow leaves only the orange inner loop; the outer loop still adds t to s and moves on to the next i.

Illustration drawn for QuestionBuddy.

Understand Series Programs and break and continue Inside Nested Loops

Series with nested loops. When the k-th term depends on k (a sum, product or factorial), compute the term in an inner loop and add it to the total in the outer loop.

  • 1 + (1+2) + (1+2+3) + ... + (1+2+...+n): inner loop adds j for j = 1 to i into t; outer loop adds t to s. For n = 3: terms 1, 3, 6, total 10.
  • 1! + 2! + 3! + 4!: for each i the inner loop forms the factorial f = f x j; the outer adds f to the sum. Total 1 + 2 + 6 + 24 = 33.

Always reset the term variable (t = 0 or f = 1) at the start of every outer pass.

Floyd-style number triangle. A counter k that is increased after every print gives consecutive numbers: row 1 has 1, row 2 has 2 3, row 3 has 4 5 6.

break and continue in nested loops.

  • break leaves only the loop that contains it. In an inner loop it ends the inner loop for that row; the outer loop continues with its next pass.
  • continue skips the rest of the current pass of the loop that contains it.

Prime check pattern. For each n, set a flag to true, try divisors d from 2 to n - 1, and if n % d == 0 set the flag to false and break. Print n if the flag is still true.

Tracing method. For each outer value write the inner loop's work in a line, including the point where break or continue acts, then collect the output.

Neighbouring topics: row and column patterns are on the previous page of this chapter.

Series Programs and break and continue Inside Nested Loops: key facts

Term reset
set t = 0 (sum) or f = 1 (product) | at the start of each outer pass
Otherwise terms carry over.
Triangular sums
1 + (1+2) + (1+2+3) for n = 3 | gives 1 + 3 + 6 = 10
Inner loop builds each bracket.
Jumps in nested loops
break and continue | affect only the loop that contains them
The outer loop continues.
Prime flag
  • flag = true
  • divisor found: flag = false and break
  • print if flag still true
Divisors from 2 to n - 1.

Series Programs and break and continue Inside Nested Loops in real life

The idea, met in an everyday situation. Answer the question in your own words, then compare with the model answer. The one-line hook is what to remember.

Weekly savings

Suppose a student saves Rs 1 on day 1, then in week 2 saves 1 and 2 on two days, and in week 3 saves 1, 2 and 3 on three days, adding up all the amounts.

What is the total saved after 3 weeks?

Try it first, then show the solutionSolution
  1. Week 1 total: 1.
  2. Week 2 total: 1 + 2 = 3.
  3. Week 3 total: 1 + 2 + 3 = 6; overall 1 + 3 + 6 = 10.

Answer: Rs 10, which is what the nested loop 1 + (1+2) + (1+2+3) computes.

Memory hook: Inner builds the week, outer adds the weeks.

Tricks to remember Series Programs and break and continue Inside Nested Loops

Short memory aids. Say each one aloud once, then use it on a question the same day.

  • Reset the term

    Start every outer pass with t = 0 or f = 1.

    Use it when: Series programs.

  • Break breaks one loop

    break leaves just the loop that holds it.

    Use it when: Nested jumps.

  • Flag and break

    A prime check uses a flag that is cleared at the first divisor.

    Use it when: Prime programs.

Series Programs and break and continue Inside Nested Loops: solved examples

Ordered from board level to HOTS. Try each one before you open the solution.

Example 1: Iteration count · 1 mark · MCQBoard level

How many times does the inner body run?

for (int i = 1; i <= 5; i++)
for (int j = 1; j <= 3; j++)
count++;

(a) 8 (b) 15 (c) 5 (d) 3

Try it first, then show the solutionSolution
  1. 5 outer passes x 3 inner passes = 15.

Answer: (b) 15

Example 2: Sum of a triangular series · 3 marks · Short answerHarder board level

Find the output:

int s = 0;
for (int i = 1; i <= 3; i++)
{
int t = 0;
for (int j = 1; j <= i; j++)
t = t + j;
s = s + t;
}
System.out.println(s);
Try it first, then show the solutionSolution
  1. i = 1: t = 1, s = 1. (1 mark)
  2. i = 2: t = 1 + 2 = 3, s = 4. (1 mark)
  3. i = 3: t = 6, s = 10. Output 10. (1 mark)

Answer: 10

Example 3: Factorial series · 3 marks · Short answerHarder board level

Find the output:

int s = 0;
for (int i = 1; i <= 4; i++)
{
int f = 1;
for (int j = 1; j <= i; j++)
f = f * j;
s = s + f;
}
System.out.println(s);
Try it first, then show the solutionSolution
  1. The inner loop gives f = 1, 2, 6, 24 for i = 1, 2, 3, 4. (1 mark)
  2. s is 1, then 3, then 9, then 33. (1 mark)
  3. Output: 33. (1 mark)

Answer: 33

Example 4: Case study: break and continue · 4 marks · Case-basedHOTS / competency

Two fragments are run:
Fragment A:

for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
if (j == i)
break;
System.out.print(j);
}
System.out.println();
}

Fragment B:

for (int i = 1; i <= 2; i++)
{
for (int j = 1; j <= 3; j++)
{
if (j == 2)
continue;
System.out.print(i * j + " ");
}
System.out.println();
}

(i) Show the output of A. (2)
(ii) Show the output of B. (1)
(iii) Which loop does break affect? (1)

Try it first, then show the solutionSolution
  1. (i) i = 1: j = 1 equals i, break, nothing printed (an empty line). i = 2: prints 1, then break at j = 2. i = 3: prints 12, then break at j = 3. Output: an empty line, 1, 12.
  2. (ii) i = 1: j = 1 gives 1, j = 2 skipped, j = 3 gives 3. i = 2: 2 and 6. Output: 1 3 and 2 6.
  3. (iii) Only the inner loop that contains it.

Answer: (i) empty line, 1, 12 (ii) 1 3 / 2 6 (iii) the inner loop only

Example 5: Prime numbers up to 10 · 3 marks · Short answerHarder board level

Find the output:

for (int n = 2; n <= 10; n++)
{
boolean prime = true;
for (int d = 2; d < n; d++)
{
if (n % d == 0)
{
prime = false;
break;
}
}
if (prime)
System.out.print(n + " ");
}
Try it first, then show the solutionSolution
  1. 2 and 3 have no divisor tested in 2 to n - 1 that divides them, so they stay prime. (1 mark)
  2. 4, 6, 8, 9 and 10 each meet a divisor (2 or 3) and break. (1 mark)
  3. 5 and 7 stay prime. Output: 2 3 5 7. (1 mark)

Answer: 2 3 5 7

Where students lose marks in Series Programs and break and continue Inside Nested Loops

The slips examiners see most often, and what to write instead.

  • Mistake: Not resetting the term

    The variable lives outside the outer loop.

    Wrong working

    t declared once before the loops

    Right working

    int t = 0; at the start of every outer pass

    Do this: Reset in the outer loop.

    Usually costs: 2 marks

  • Mistake: Assuming break ends both loops

    Single break is misread.

    Wrong working

    break stops the whole nested loop

    Right working

    break stops only the inner loop

    Do this: It affects the nearest loop.

    Usually costs: 2 marks

  • Mistake: Missing flag in a prime check

    Printing happens inside the divisor loop.

    Wrong working

    Printing n for every d that does not divide

    Right working

    Use a flag and print after the inner loop

    Do this: Decide after the loop ends.

    Usually costs: 2 marks

Exam tips

  • Show a line for each outer value when tracing nested loops.
  • Name the variable that is reset at each outer pass.
  • Mention that break and continue affect only the nearest loop.
  • Check small cases (n = 2, n = 3) of a prime program.

Practice questions on Series Programs and break and continue Inside Nested Loops

Write each answer on paper first. Open the hint only if you are stuck, and the answer only when you have finished.

  1. Q1 · 1 mark · MCQBoard level

    A break statement inside an inner loop terminates:
    (a) the whole program (b) only the inner loop (c) both loops (d) only the current outer pass without leaving the inner loop

    Hint

    It leaves the loop that holds it.

    Answer

    (b) only the inner loop

  2. Q2 · 1 mark · Very short answerBoard level

    How many times does the body run for i = 1 to 4 and j = 1 to 4?

    Hint

    Rows times columns.

    Answer

    16

  3. Q3 · 3 marks · Short answerHarder board level

    Find the output:

    int s = 0;
    for (int i = 1; i <= 4; i++)
    {
    int p = 1;
    for (int j = 1; j <= i; j++)
    p = p * 2;
    s = s + p;
    }
    System.out.println(s);
    Hint

    p takes 2, 4, 8, 16.

    Answer

    30 (2 + 4 + 8 + 16)

  4. Q4 · 3 marks · Short answerHarder board level

    Show the output:

    for (int i = 2; i <= 3; i++)
    {
    for (int j = 1; j <= 3; j++)
    System.out.print(i * j + " ");
    System.out.println();
    }
    Hint

    Multiply i by j.

    Answer

    2 4 6
    3 6 9

  5. Q5 · 4 marks · Short answerHOTS / competency

    Write nested loops that print Floyd's triangle of three rows and show the output.

    Hint

    Use a counter k.

    Answer

    int k = 1;
    for (int i = 1; i <= 3; i++)
    {
    for (int j = 1; j <= i; j++)
    {
    System.out.print(k + " ");
    k++;
    }
    System.out.println();
    }
    Output:
    1
    2 3
    4 5 6

Test your recall

Answer in your head or on paper first, then open the card. Pulling an answer out of memory makes it stick far better than re-reading.

  1. 1.Sum of 1 + (1+2) + (1+2+3)?Show answer

    10

  2. 2.What is 4 factorial?Show answer

    24

  3. 3.Which loop does break affect?Show answer

    The nearest enclosing loop.

  4. 4.Why use a flag in a prime check?Show answer

    To decide after the divisor loop whether any divisor was found.

  5. 5.Divisors tested for n in a simple prime check?Show answer

    2 to n - 1

  6. 6.How is Floyd's triangle numbered?Show answer

    With a counter that increases after every print.

Series Programs and break and continue Inside Nested Loops: frequently asked questions

Can continue be used in the outer loop?

Yes, it then skips the rest of that outer pass.

Why is 2 prime in this program?

The inner loop does not run for n = 2, so no divisor is found.

Is 1 prime?

No; the program starts from 2.