Operators, Control Flow and Number Programs in Java (ISC Class 12 Computer Science)
Updated 18 October 2026 · Draft, not yet fact-checked
Output questions rely on integer division, the modulus operator, pre and post increments and operator precedence. A switch without break falls through to the next case. Number programs extract digits with % 10 and / 10 inside a loop, then apply a test such as the sum of cubes (Armstrong), the sum of proper divisors (perfect) or the split of a square (Kaprekar).
Each loop pass takes the last digit with % 10, then / 10 removes it; stop at 0, and here the cubes sum back to 153.
Illustration drawn for QuestionBuddy.
Understand Operators, Control Flow and Number Programs in Java
1. Operators
/on two ints gives an int (10 / 4 = 2); with a double operand gives a double (10 / 4.0 = 2.5).%gives the remainder (10 % 4 = 2).x++uses x and then adds 1;++yadds 1 and then uses y.- Compound assignment:
x += y * 2means x = x + (y * 2); the right side is evaluated first. - Precedence: parentheses, then unary and increments, then * / %, then + -, then comparisons, then && and ||, then assignment.
Traced example. With int x = 10, y = 4:
System.out.println(x / y);
System.out.println(x % y);
System.out.println(x / (double) y);
System.out.println(x++ + y);
System.out.println(++y * x);
x += y * 2;
System.out.println(x);Hand trace: x / y = 2; x % y = 2; x / (double) y = 2.5; x++ + y = 10 + 4 = 14 and x becomes 11; ++y makes y = 5, then 5 * 11 = 55; x += y * 2 gives 11 + 10 = 21. Output lines: 2, 2, 2.5, 14, 55, 21. (Traced by hand, not compiled.)
2. Switch and fall-through. Without break, execution continues into the next case. For k = 3 with case 1: s += 1; case 3: s += 3; case 4: s += 4; break; default: s += 9; and s = 0: case 3 adds 3 (s = 3), falls into case 4 and adds 4 (s = 7), then break. s = 7.
3. Loops. For for (i = 1; i <= 10; i += 3) sum += i; the values of i are 1, 4, 7, 10: four iterations, sum = 22. The loop stops when i becomes 13.
4. Digit extraction. While n > 0: d = n % 10 (last digit), then n = n / 10 (remove it). The same loop gives the sum of digits, the count of digits and the reverse.
5. Special numbers
- Armstrong (3 digits): sum of the cubes of the digits equals the number. 153: 1 + 125 + 27 = 153 yes; 370: 27 + 343 + 0 = 370 yes; 123: 1 + 8 + 27 = 36 no.
- Perfect: the sum of proper divisors (excluding the number) equals the number. 28: 1 + 2 + 4 + 7 + 14 = 28 yes; 12: 1 + 2 + 3 + 4 + 6 = 16, no.
- Kaprekar: split the square into a right part with as many digits as the number and a left part; their sum equals the number. 45: 45 x 45 = 2025; 20 + 25 = 45 yes. 9: 81; 8 + 1 = 9 yes. 10: 100; 1 + 00 = 1, no.
6. Common loop faults. Wrong bound (n > 9 instead of n > 0), forgetting to update a variable, or testing a divisor up to the number instead of half of it.
Operators, Control Flow and Number Programs in Java: key facts
- Increment forms
- x++ uses then adds | ++x adds then uses
- Check which value appears.
- Digit extraction
- d = n % 10
- n = n / 10
- repeat while n > 0
- Basis of digit programs.
- Special numbers
Armstrong sum of cubes Perfect sum of proper divisors Kaprekar split the square - All equal the number.
How to answer Operators, Control Flow and Number Programs in Java questions
Questions give a snippet and ask for the output, or ask for the logic of a number program.
- 1Write each variable's value after each statement in a trace table.
- 2For increments, decide if the old or the new value is used in the expression.
- 3For switch, find the matching case and continue until a break or the end.
- 4For number tests, compute the defining quantity and compare it with the number.
Operators, Control Flow and Number Programs in Java 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.
Post and pre increment
Suppose a ticket counter shows the current token as x = 10 and issues the next with x++, while a second counter uses ++x on the same value.
What token does each issue first if both start from 10?
Try it first, then show the solutionSolution
- x++ issues 10 and then makes x 11.
- ++x makes x 11 and then issues 11.
Answer: x++ issues 10; ++x issues 11.
Memory hook: Post uses first; pre adds first.
Digit sum of a bill number
Imagine a clerk checks bill number 4075 by adding its digits to get a check value.
What is the check value?
Try it first, then show the solutionSolution
- 4075 % 10 = 5, then 407; 407 % 10 = 7, then 40; 40 % 10 = 0, then 4; 4 % 10 = 4, then 0.
- 5 + 7 + 0 + 4 = 16.
Answer: 16
Memory hook: Peel digits from the right.
Tricks to remember Operators, Control Flow and Number Programs in Java
Short memory aids. Say each one aloud once, then use it on a question the same day.
Post afterwards, Pre first
x++ gives the old value; ++x gives the new value.
Use it when: Increment questions.
Fall through until break
Cases run on until a break stops them.
Use it when: Switch questions.
Operators, Control Flow and Number Programs in Java: solved examples
Ordered from board level to HOTS. Try each one before you open the solution.
Example 1: Integer division · 1 mark · MCQBoard level
The value of 17 / 5 + 17 % 5 in Java is:
(a) 3
(b) 5
(c) 5.4
(d) 6
Try it first, then show the solutionSolution
- 17 / 5 = 3 (integer division); 17 % 5 = 2; 3 + 2 = 5.
Answer: (b)
Example 2: Switch fall-through · 2 marks · Short answerBoard level
Give the value of s after: int s = 0, k = 3; switch (k) { case 1: s += 1; case 3: s += 3; case 4: s += 4; break; default: s += 9; }
Try it first, then show the solutionSolution
- k = 3 matches case 3: s = 0 + 3 = 3.
- No break, so execution falls into case 4: s = 3 + 4 = 7; the break stops it.
Answer: 7
Example 3: Trace with increments · 4 marks · Short answerHarder board level
With int x = 9, y = 5, give the output of: System.out.println(x++ + y); System.out.println(++y * x); x += y * 2; System.out.println(x);
Try it first, then show the solutionSolution
- x++ + y uses x = 9: 9 + 5 = 14; then x = 10.
- ++y makes y = 6; 6 * 10 = 60.
- x += y * 2: y * 2 = 12; x = 10 + 12 = 22.
- Output lines: 14, 60, 22.
Answer: 14, 60 and 22.
Example 4: Special number tests · 4 marks · Short answerHarder board level
State with working whether each is an Armstrong number: 153, 370, 371, 123. Then show that 28 is a perfect number and 12 is not.
Try it first, then show the solutionSolution
- 153: 1 + 125 + 27 = 153 yes. 370: 27 + 343 + 0 = 370 yes. 371: 27 + 343 + 1 = 371 yes. 123: 1 + 8 + 27 = 36 no.
- 28: proper divisors 1, 2, 4, 7, 14 add to 28: perfect.
- 12: 1 + 2 + 3 + 4 + 6 = 16, which is not 12: not perfect.
Answer: 153, 370, 371 are Armstrong, 123 is not; 28 perfect, 12 not.
Example 5: Design a Kaprekar check · 8 marks · Long answerHOTS / competency
Write the logic of a method that decides whether a number n is a Kaprekar number and trace it for n = 45 and n = 10. State the divisor you use to split the square and why.
Try it first, then show the solutionSolution
- Logic: sq = n * n; count the digits d of n; p = 10 raised to d (computed by a loop); right = sq % p; left = sq / p; if left + right == n then Kaprekar.
- Why p: the right part has as many digits as n.
- n = 45: sq = 2025, d = 2, p = 100; right = 25, left = 20; 20 + 25 = 45: Kaprekar.
- n = 10: sq = 100, d = 2, p = 100; right = 0, left = 1; 1 + 0 = 1, not 10: not Kaprekar.
- Special case n = 1: sq = 1, d = 1, p = 10; right = 1, left = 0; sum 1: Kaprekar.
- Use long for the square to avoid overflow for larger numbers.
- Test values: 9 (81: 8 + 1 = 9, yes), 45, 10 and 1.
- The program was not compiled; the traces are by hand.
Answer: 45 is Kaprekar, 10 is not; split by 10 raised to the number of digits of n.
Where students lose marks in Operators, Control Flow and Number Programs in Java
The slips examiners see most often, and what to write instead.
Mistake: Using the new value for a post-increment
x++ uses the old value in the expression.
Wrong working
x++ + y with x = 10, y = 4 gives 15.
Right working
x++ + y = 10 + 4 = 14, and x becomes 11 afterwards.
Do this: Post uses then adds.
Usually costs: 1-2 marks.
Mistake: Ignoring fall-through
No break lets the next case run.
Wrong working
s = 3 for k = 3.
Right working
s = 3 + 4 = 7 because case 3 falls into case 4.
Do this: Look for break.
Usually costs: 1-2 marks.
Mistake: Including the number itself among proper divisors
Proper divisors exclude the number.
Wrong working
28: 1 + 2 + 4 + 7 + 14 + 28 = 56.
Right working
28: 1 + 2 + 4 + 7 + 14 = 28.
Do this: Stop at n / 2.
Usually costs: 1 mark.
Exam tips
- Trace each variable line by line in output questions.
- Write intermediate values to earn method marks.
- State the definition of the special number before the logic.
- Name test values including a boundary.
Practice questions on Operators, Control Flow and Number Programs in Java
Write each answer on paper first. Open the hint only if you are stuck, and the answer only when you have finished.
Q1 · 1 mark · Very short answerBoard level
What is the value of 7 / 2 in Java when both are ints?
Hint
No decimals.
Answer
3
Q2 · 2 marks · Short answerBoard level
Find the number of iterations and the sum for: for (int i = 2; i <= 20; i += 6) sum += i;
Hint
List the values of i.
Answer
i takes 2, 8, 14, 20: four iterations. Sum = 2 + 8 + 14 + 20 = 44.
Q3 · 2 marks · Short answerHarder board level
Why do we test for divisors only up to half of n when finding the sum of proper divisors?
Hint
Largest proper divisor.
Answer
The largest proper divisor of n (other than n itself) cannot exceed n / 2, so testing beyond n / 2 would only waste time.
Q4 · 4 marks · Short answerHarder board level
With int a = 8, b = 3: give the output of System.out.println(a++ * b); System.out.println(a-- - --b); System.out.println(a + b);
Hint
Track a and b after each line.
Answer
Line 1: 8 x 3 = 24; then a = 9. Line 2: a-- uses 9 (a becomes 8); --b makes b = 2; 9 - 2 = 7. Line 3: a + b = 8 + 2 = 10. Output: 24, 7, 10.
Q5 · 4 marks · Short answerHOTS / competency
A student's Armstrong program counts digits using 'while (n > 9)' and gets the wrong power for the digits. Explain the effect for n = 153 and give the correct approach for 3-digit Armstrong numbers.
Hint
Digit count off by one.
Answer
With n > 9 the loop counts 153, 15 and stops at 1, giving 2 digits instead of 3, so cubes would not be used and the test fails. For 3-digit Armstrong numbers use the cube of each digit with a correct loop (while n > 0): 1 + 125 + 27 = 153.
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.What is 10 % 4?Show answer
2.
2.What does x += 5 mean?Show answer
x = x + 5.
3.Is 153 Armstrong?Show answer
Yes: 1 + 125 + 27 = 153.
4.What stops a switch from falling through?Show answer
A break statement.
Operators, Control Flow and Number Programs in Java: frequently asked questions
Is the order of evaluation left to right?
Operands are evaluated left to right, but precedence decides grouping.
Are 1 and 9 Kaprekar numbers?
Yes: 1 x 1 = 1 and 9 x 9 = 81 with 8 + 1 = 9.
Do these topics repeat in later chapters?
Yes; arrays, strings and recursion use the same loops and operators.