Conditional and Looping Statements, Blocks and Scope (ISC Class 12 Computer Science)
Updated 17 October 2026 · Draft, not yet fact-checked
Java chooses with if, if-else, if-else-if, switch and the ternary operator, and repeats with for, while and do-while. break leaves the nearest loop or switch, continue skips to the next iteration. A variable lives only inside the block { } where it is declared. In a switch, a missing break makes control fall through to the next case.
Notice where the Test? diamond sits: while tests first and may run zero times, but do-while always runs the body at least once.
Illustration drawn for QuestionBuddy.
Understand Conditional and Looping Statements, Blocks and Variable Scope
Conditional statements.
if (cond) stmt;andif (cond) ... else ...choose one of two paths;if ... else if ... elsechooses one of many (the first true condition wins).switch (expr)compares the value (int, char, String or an enum) with eachcaselabel. When a match is found execution continues through the following statements until abreak(fall-through).defaultruns when nothing matches. Case labels must be constants.- Ternary:
cond ? valueIfTrue : valueIfFalse.
Loops.
for (init; cond; update)is the usual choice when the number of repeats is known; the three parts may be empty.while (cond)tests first and may run zero times.do { ... } while (cond);(exit-controlled: the body runs first, then the test) runs the body at least once.- break ends the nearest enclosing loop or switch; continue skips the rest of the current iteration and moves to the update and test of the same loop. In a nested loop each applies to the innermost loop only.
Blocks and scope. A block is a group of statements in braces. A variable declared inside a block (including a for-loop header) is visible only from its declaration to the closing brace; using it outside is a compile error. Variables declared in the class outside methods are visible to all methods; a local variable declared in a method may not reuse the name of another local variable that is still in scope. Initialise local variables before use: Java does not give them default values.
Tracing nested loops. Keep a table of the loop variables and the output after each iteration. The inner loop restarts from its initial value every time the outer loop moves on.
Operators and precedence are in the previous chapter; loops reappear in arrays and in recursion as alternatives.
Conditional and Looping Statements, Blocks and Variable Scope: key facts
- Loop choice
for known number of repeats while test first, may run zero times do-while runs at least once Exit-controlled means do-while - Choose by when the test is made.
- break and continue
- break leaves the nearest loop or switch
- continue skips to the next iteration
- Both affect only the innermost loop
- Use a flag to leave an outer loop.
- switch rules
- Case labels are constants
- No break means fall-through
- default runs when no case matches
- Allowed types: int, char, String, enum and similar
- Execution continues from the matched label.
- Scope of a variable
- Block variable: from its declaration to the closing brace
- Loop variable declared in for: only inside the loop
- Local variables get no default value
- Using a variable outside its block is a compile error.
How to answer Conditional and Looping Statements, Blocks and Variable Scope questions
Use this routine to trace loop and switch programs.
- 1Make a trace table with a column for each variable that changes.
- 2For a for loop, run init once, then repeat test, body, update.
- 3For a switch, find the matching label and carry on downwards until a break or the end.
- 4In nested loops, finish the inner loop completely for each outer value.
- 5Apply break and continue to the innermost loop only.
- 6Write the output with the exact spacing and line breaks (print versus println).
Conditional and Looping Statements, Blocks and Variable Scope 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.
Attendance counter that skips a holiday
Suppose a program loops over days 1 to 5 and counts the lessons held, skipping day 2 (a holiday) with continue.
If each remaining day has 2 lessons, how many lessons are counted?
Try it first, then show the solutionSolution
- Days 1, 3, 4, 5 are processed: four days.
- 4 x 2 = 8 lessons.
Answer: 8 lessons.
Memory hook: continue skips just this round, not the whole loop.
Grade message with fall-through
Suppose a grade program forgets break after the case for grade B and prints a message for B and C together.
What kind of error is this and how is it fixed?
Try it first, then show the solutionSolution
- Without break execution falls into the next case.
- Add break after each case body.
Answer: A logic error (fall-through); add break at the end of each case.
Memory hook: No break, no stop.
Tricks to remember Conditional and Looping Statements, Blocks and Variable Scope
Short memory aids. Say each one aloud once, then use it on a question the same day.
Entry and exit control
for and while test at the entry; do-while tests at the exit.
Use it when: Entry- versus exit-controlled loop questions.
break is the exit sign
break leaves the loop or switch; continue only skips the current round.
Use it when: Distinguishing the two.
Inner loop restarts
Every outer round, the inner loop starts fresh from its init.
Use it when: Nested loop tracing.
Scope is the braces
A name dies at the closing brace of the block where it was born.
Use it when: Scope questions.
Conditional and Looping Statements, Blocks and Variable Scope: solved examples
Ordered from board level to HOTS. Try each one before you open the solution.
Example 1: Skipping an iteration · 1 mark · MCQBoard level
Which statement skips the remaining statements of the current iteration and goes on to the next one?
(a) break (b) exit (c) continue (d) return
Try it first, then show the solutionSolution
- break leaves the loop; continue only skips the rest of the current pass.
Answer: (c) continue
Example 2: Switch with fall-through · 3 marks · Short answerHarder board level
Write the output:
int n = 2, s = 0;
switch (n) {
case 1: s += 1;
case 2: s += 2;
case 3: s += 3; break;
case 4: s += 4;
default: s += 10;
}
System.out.println(s);Try it first, then show the solutionSolution
- n = 2 matches case 2: s = 2. (1 mark)
- There is no break, so case 3 also runs: s = 5, then break leaves the switch. (1.5 marks)
- Output: 5. (0.5 mark)
Answer: 5
Example 3: Nested loop with break and continue · 4 marks · Short answerHarder board level
Write the output:
int count = 0;
for (int i = 1; i <= 5; i++) {
if (i == 2) continue;
for (int j = 1; j <= i; j++) {
if (j == 3) break;
count++;
}
}
System.out.println(count);Try it first, then show the solutionSolution
- i = 1: inner loop runs j = 1 only, count = 1. (1 mark)
- i = 2 is skipped by continue. i = 3: j = 1, 2 add 2, then j = 3 breaks, count = 3. (1 mark)
- i = 4: j = 1, 2 add 2, count = 5. i = 5: j = 1, 2 add 2, count = 7. (1.5 marks)
- Output: 7. (0.5 mark)
Answer: 7
Example 4: do-while trace · 3 marks · Short answerHarder board level
Write the output:
int x = 10;
do {
System.out.print(x + " ");
x -= 3;
} while (x > 4);
System.out.println(x);Try it first, then show the solutionSolution
- First pass prints 10, x = 7; 7 > 4, so repeat. (1 mark)
- Second pass prints 7, x = 4; 4 > 4 is false, so stop. (1 mark)
- println prints the final x = 4. Output: 10 7 4. (1 mark)
Answer: 10 7 4
Example 5: Case: loop result and scope · 4 marks · Case-basedHOTS / competency
Suppose a program contains:
int total = 0;
for (int i = 1; i <= 4; i++) {
int sq = i * i;
if (sq % 2 == 0)
total += sq;
}
System.out.println(total);
System.out.println(i);(i) Find total. (1 mark)
(ii) Why does the last line fail to compile? (1 mark)
(iii) Rewrite the loop using while so that i is usable afterwards. (2 marks)
Try it first, then show the solutionSolution
- (i) Squares 1, 4, 9, 16; the even squares 4 and 16 are added: total = 20.
- (ii) i is declared in the for header, so its scope ends with the loop; outside it the name is unknown (cannot find symbol).
- (iii)
After the loop i is 5 and println(i) prints 5.int i = 1; while (i <= 4) { int sq = i * i; if (sq % 2 == 0) total += sq; i++; }
Answer: (i) 20 (ii) i is out of scope after the for loop (iii) declare int i = 1 before a while loop and use i++ inside; i is then 5
Where students lose marks in Conditional and Looping Statements, Blocks and Variable Scope
The slips examiners see most often, and what to write instead.
Mistake: Forgetting fall-through
Without break, execution runs on into the next case.
Wrong working
Only case 2 runs when n = 2
Right working
case 2 and then case 3 run until the break
Do this: Trace downwards from the matching label.
Usually costs: 1 mark
Mistake: Applying break to the outer loop
break stops only the innermost loop or switch.
Wrong working
break in the inner loop ends the whole program section
Right working
Only the inner loop ends; the outer loop continues
Do this: Use a flag or label to leave both loops.
Usually costs: 1 mark
Mistake: Using a loop variable after the loop
A variable declared in the for header does not exist outside it.
Wrong working
for (int i = 0; ...) {...} System.out.println(i);
Right working
int i; for (i = 0; ...) {...} System.out.println(i);
Do this: Declare the variable before the loop.
Usually costs: 1 mark
Exam tips
- Draw a trace table with one column per variable.
- Write each print on its own output line; show line breaks for println.
- State whether a loop is entry-controlled or exit-controlled when asked.
- Put break after every case unless fall-through is intended.
- Declare the counter before the loop if it is needed afterwards.
Practice questions on Conditional and Looping Statements, Blocks and Variable Scope
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 · MCQBoard level
Which loop is guaranteed to execute its body at least once?
(a) for (b) while (c) do-while (d) enhanced forHint
The test comes after the body.
Answer
(c) do-while
Q2 · 2 marks · Short answerBoard level
Differentiate between break and continue with a small example of each.
Hint
One leaves the loop, the other skips a round.
Answer
break ends the nearest loop or switch immediately: for (i = 1; i <= 5; i++) { if (i == 3) break; } stops at 3. continue skips the rest of the current iteration and goes on to the next: with if (i == 3) continue; the loop runs for i = 1, 2, 4, 5 with only 3 skipped.
Q3 · 4 marks · Short answerHarder board level
Write a program segment that reads an integer n and prints the sum of its digits. Trace it for n = 4729.
Hint
Use % 10 and / 10 in a loop.
Answer
int sum = 0; int t = n; while (t > 0) { sum += t % 10; t /= 10; } System.out.println(sum);For 4729: digits 9, 2, 7, 4 are added in turn: 9, 11, 18, 22. t goes 4729, 472, 47, 4, 0. Output: 22
Q4 · 3 marks · Short answerHarder board level
Write the output:
for (int i = 1; i <= 3; i++) { for (int j = i; j <= 3; j++) System.out.print(j); System.out.println(); }Hint
The inner loop starts from i.
Answer
i = 1 prints 1, 2, 3 and a line break; i = 2 prints 2, 3; i = 3 prints 3. Output:
123
23
3Q5 · 3 marks · Case-basedHOTS / competency
Write the output of the code below and rewrite it so that only the text for grade B is printed.
char g = 'B'; String r = ""; switch (g) { case 'A': r += "Excellent "; case 'B': r += "Good "; case 'C': r += "Fair "; break; default: r += "Unknown"; } System.out.println(r);Hint
Where does execution continue after the match?
Answer
g = B matches case B: r = "Good ". There is no break, so case C also runs: r = "Good Fair ", then break. Output: Good Fair (with a trailing space). Fix: add break; after r += "Good "; in case B (and after case A) so the output is Good.
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.Name the exit-controlled loop in Java.Show answer
do-while.
2.What does default do in a switch?Show answer
Runs when no case label matches.
3.What does continue do?Show answer
Skips the rest of the current iteration of the innermost loop.
4.What is the ternary operator?Show answer
cond ? valueIfTrue : valueIfFalse.
5.What is the scope of a variable declared in a for header?Show answer
Only the for loop.
6.What happens when a case has no break?Show answer
Execution falls through into the next case.
Conditional and Looping Statements, Blocks and Variable Scope: frequently asked questions
What is the difference between while and do-while?
while tests the condition before each pass and may not run at all; do-while runs the body once first and tests afterwards, so it runs at least once.
What is fall-through in a switch statement?
When a matching case has no break, execution continues into the following case labels until a break or the end of the switch.
What is the scope of a variable in Java?
The region of the program where the variable can be used: a local variable is visible from its declaration to the end of its block.
Can continue be used inside a switch?
Only when the switch is inside a loop; continue then skips to the next iteration of that loop.