Skip to content
Computer ApplicationsICSE Class 9 · Looping (Iterative Statements) in Java

break, continue, Digit Extraction and Series Programs in Java (ICSE Class 9)

Updated 18 October 2026 · Fact-checked

break ends the loop at once, and continue skips the rest of the current pass and goes to the next one. Digits of a number are taken with n % 10 (the last digit) and n / 10 (the number without its last digit) inside a loop, which gives programs for the sum of digits, reversing a number, palindromes and Armstrong numbers. Series programs use an accumulator that adds or multiplies a term in each pass.

Each pass, n % 10 picks off the last digit and n / 10 drops it; the loop stops when n becomes 0.

Illustration drawn for QuestionBuddy.

Understand break, continue, Digit Extraction and Series Programs Using Loops

Jump statements.

  • break: leaves the nearest enclosing loop (or switch) immediately. Execution continues after the loop.
  • continue: skips the remaining statements of the current pass and goes to the next pass. In a for loop the update still runs; in a while loop the update must come before the continue or the loop may never end.

Digit extraction. For an int n:

  • n % 10 is the last digit; n / 10 removes the last digit (integer division).
  • Repeat while n > 0 (or n != 0).

Count digits: c++ each pass. Sum of digits: s = s + n % 10. Reverse: rev = rev * 10 + n % 10. For a palindrome test keep the original in another variable (t = n) because n is destroyed by the loop, then compare rev with t.
Armstrong number (of three digits): the sum of the cubes of the digits equals the number; for 153, 1 + 125 + 27 = 153.

Accumulators in series programs. Start the sum at 0 and the product at 1. Factorial: f = f * i for i from 1 to n. Use a long or double when values grow, and divide by a decimal when terms are fractions (1.0 / i).

Plan. Decide the loop type (known number of passes: for; unknown: while), the starting values of the accumulators and the update.

Neighbouring topics: the three loop forms and their conversion are on the previous page of this chapter.

break, continue, Digit Extraction and Series Programs Using Loops: key facts

Digit operations
  • last digit: n % 10
  • drop the last digit: n / 10
  • loop while n > 0
Integer division is needed.
Reverse
  • rev = rev times 10 + n % 10
  • start rev = 0
  • keep the original in t
Compare rev with t for a palindrome.
Accumulators
  • sum starts at 0
  • product starts at 1
  • factorial f = f times i
Use long for large factorials.
Jumps
break: leave the loop | continue: skip to the next pass
Only the nearest loop is affected.

How to answer break, continue, Digit Extraction and Series Programs Using Loops questions

Use this routine for digit-based programs.

  1. 1Save the original number if it is needed later.
  2. 2Set the accumulators (sum, count, reverse) to their starting values.
  3. 3Loop while n > 0: take d = n % 10, update the accumulators, set n = n / 10.
  4. 4After the loop, use the results; compare with the saved number for palindrome or Armstrong checks.

break, continue, Digit Extraction and Series Programs Using 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.

Peeling digits

Suppose a cashier reads a bill number 2706 from the right, one digit at a time, to add the digits as a check sum.

What is the check sum and how many passes does the loop make?

Try it first, then show the solutionSolution
  1. The digits from the right are 6, 0, 7, 2.
  2. The sum is 6 + 0 + 7 + 2 = 15.
  3. Each pass removes one digit: 4 passes.

Answer: Check sum 15 after 4 passes.

Memory hook: Mod takes it, divide drops it.

Tricks to remember break, continue, Digit Extraction and Series Programs Using Loops

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

  • Mod takes, div drops

    n % 10 takes the last digit and n / 10 drops it.

    Use it when: Digit programs.

  • Break leaves, continue skips

    break leaves the loop; continue skips one pass.

    Use it when: Jump statements.

  • Save before you destroy

    Copy n before a digit loop if you need it afterwards.

    Use it when: Palindromes.

break, continue, Digit Extraction and Series Programs Using Loops: solved examples

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

Example 1: Effect of break · 1 mark · MCQBoard level

What is printed?

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

(a) 0 (b) 012 (c) 02 (d) 01

Try it first, then show the solutionSolution
  1. i = 0 is printed; at i = 1 break ends the loop.

Answer: (a) 0

Example 2: Sum of digits · 3 marks · Short answerHarder board level

Trace the sum of digits of n = 2706:

int n = 2706, s = 0;
while (n > 0)
{
s = s + n % 10;
n = n / 10;
}
System.out.println(s);
Try it first, then show the solutionSolution
  1. Pass 1: digit 6, s = 6, n = 270. Pass 2: digit 0, s = 6, n = 27. (1 mark)
  2. Pass 3: digit 7, s = 13, n = 2. Pass 4: digit 2, s = 15, n = 0. (1 mark)
  3. n > 0 is false, so the loop ends; output 15. (1 mark)

Answer: 15

Example 3: break and continue together · 3 marks · Short answerHarder board level

Find the output:

for (int i = 1; i <= 6; i++)
{
if (i == 4)
break;
if (i % 2 == 0)
continue;
System.out.print(i + " ");
}
Try it first, then show the solutionSolution
  1. i = 1: not 4, not even, so 1 is printed. i = 2: even, continue (nothing printed). (1 mark)
  2. i = 3: printed. (1 mark)
  3. i = 4: break ends the loop, so 5 and 6 are never reached. Output: 1 3. (1 mark)

Answer: 1 3

Example 4: Case study: palindrome check · 4 marks · Case-basedHOTS / competency

A program checks the number 1331:

int n = 1331, t = n, rev = 0;
while (n > 0)
{
rev = rev * 10 + n % 10;
n = n / 10;
}
if (rev == t)
System.out.println("Palindrome");
else
System.out.println("Not a palindrome");

(i) Show the values of rev after each pass. (2)
(ii) State the output. (1)
(iii) Why is t needed? (1)

Try it first, then show the solutionSolution
  1. (i) Pass 1: rev = 1; pass 2: rev = 13; pass 3: rev = 133; pass 4: rev = 1331.
  2. (ii) rev equals t (1331), so the output is Palindrome.
  3. (iii) n becomes 0 at the end of the loop, so the original number must be saved in t for the comparison.

Answer: (i) 1, 13, 133, 1331 (ii) Palindrome (iii) n is destroyed by the loop

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

Find the value of f printed:

long f = 1;
for (int i = 1; i <= 5; i++)
f = f * i;
System.out.println(f);
Try it first, then show the solutionSolution
  1. After i = 1, 2, 3, 4, 5, f takes the values 1, 2, 6, 24, 120. (2 marks)
  2. The loop ends and 120 is printed. (1 mark)

Answer: 120

Where students lose marks in break, continue, Digit Extraction and Series Programs Using Loops

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

  • Mistake: Using the changed n in a comparison

    n is zero after the loop.

    Wrong working

    if (rev == n)

    Right working

    keep t = n and use if (rev == t)

    Do this: Save the original.

    Usually costs: 2 marks

  • Mistake: Wrong starting value

    The product accumulator starts at 0.

    Wrong working

    long f = 0; f = f * i

    Right working

    long f = 1;

    Do this: Product starts at 1, sum at 0.

    Usually costs: 1 mark

  • Mistake: continue before the update in a while loop

    The loop stays on the same value.

    Wrong working

    continue; placed before i++ in a while loop

    Right working

    Update first or use a for loop

    Do this: Check the update.

    Usually costs: 2 marks

Exam tips

  • Show the table of n, digit and the accumulator for each pass.
  • Write the loop condition as n > 0 (or n != 0).
  • Always say what each accumulator starts with.
  • State the output at the end of every trace.

Practice questions on break, continue, Digit Extraction and Series Programs Using 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

    The statement that terminates the nearest loop immediately is:
    (a) continue (b) break (c) skip (d) stop

    Hint

    It is also used in switch.

    Answer

    (b) break

  2. Q2 · 2 marks · Very short answerBoard level

    Write a loop that adds the even numbers from 2 to 20 and state the sum.

    Hint

    Step of 2.

    Answer

    int s = 0;
    for (int i = 2; i <= 20; i += 2)
    s += i;
    Sum = 2 + 4 + ... + 20 = 110

  3. Q3 · 3 marks · Short answerHarder board level

    Write a loop to count the digits of n = 90210 and state the count.

    Hint

    Drop one digit per pass.

    Answer

    int n = 90210, c = 0;
    while (n != 0)
    {
    c++;
    n = n / 10;
    }
    The loop runs 5 times (90210, 9021, 902, 90, 9), so c = 5.

  4. Q4 · 3 marks · Short answerHarder board level

    Find the output:

    for (int i = 1; i <= 7; i++)
    {
    if (i % 3 == 0)
    continue;
    System.out.print(i + " ");
    }
    Hint

    Skip multiples of 3.

    Answer

    1 2 4 5 7

  5. Q5 · 4 marks · Short answerHOTS / competency

    A number is perfect if the sum of its factors smaller than the number equals the number. Write a loop that finds this sum for n and show that 28 is perfect but 12 is not.

    Hint

    Test i from 1 to n / 2 with n % i == 0.

    Answer

    int s = 0;
    for (int i = 1; i <= n / 2; i++)
    {
    if (n % i == 0)
    s = s + i;
    }
    For 28: 1 + 2 + 4 + 7 + 14 = 28, so it is perfect. For 12: 1 + 2 + 3 + 4 + 6 = 16, which is not 12, so it is not perfect.

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.Which expression gives the last digit of n?Show answer

    n % 10

  2. 2.Which expression removes the last digit?Show answer

    n / 10

  3. 3.Starting value of a product accumulator?Show answer

    1

  4. 4.Starting value of rev in a reverse program?Show answer

    0

  5. 5.Which statement leaves the loop?Show answer

    break

  6. 6.What is 5 factorial?Show answer

    120

break, continue, Digit Extraction and Series Programs Using Loops: frequently asked questions

Why does the digit loop use integer division?

So that n / 10 drops the last digit instead of giving a decimal.

Can break be used outside loops and switch?

No.

What if n is negative?

The loop condition n > 0 is false at once; such programs usually assume positive input.