Java Loops, break, continue and Scope of Variables (ISC Class 11 Computer Science)
Updated 17 October 2026 · Fact-checked
Java has three loops: for (initialise; test; update), the entry-controlled while, and the exit-controlled do-while that runs its body at least once. break leaves the loop; continue skips to the next iteration. Nested loops repeat the inner loop for every pass of the outer. A variable's scope is the part of the program where it can be used: a local variable only within its block, a for-loop variable only within the loop, an instance variable throughout the object.
In while the test comes before the body, so it may run zero times; in do-while the body comes first, so it runs at least once.
Illustration drawn for QuestionBuddy.
Understand Loops in Java: for, while, do-while, break, continue, Nested Loops and Scope
for. for (int i = 1; i <= 5; i++) sum += i; gives 15. The three parts: initialisation (once), test (before each pass), update (after each pass). Any part may be omitted: for (;;) is an infinite loop.
while (entry-controlled). Tests first, may run zero times.
int n = 4271, s = 0;
while (n > 0) { s += n % 10; n /= 10; } // s = 14do-while (exit-controlled). Runs the body first, then tests; at least one pass.
int i = 10;
do { System.out.print(i + " "); i++; } while (i < 5); // prints 10break and continue.
for (int i = 1; i <= 10; i++) {
if (i % 3 == 0) continue; // skip multiples of 3
if (i > 7) break; // stop the loop
System.out.print(i + " ");
} // 1 2 4 5 7Nested loops. for (int i = 1; i <= 3; i++) { for (int j = 1; j <= i; j++) System.out.print("*"); System.out.println(); } prints *, , * (the inner body runs 1 + 2 + 3 = 6 times).
Tracing. Use a table of the variables for each pass. for (int i = 1, s = 0; i <= 4; i++) s += i * i; gives s = 1 + 4 + 9 + 16 = 30.
Scope and visibility.
- *Local variable:* declared in a method or block; visible only from its declaration to the end of the block.
- *Loop variable:* a variable declared in the for header exists only in the loop.
- *Instance variable:* belongs to an object; visible in all its methods; a local variable with the same name hides it (this.x reaches the field).
- A name cannot be declared again in an inner block of the same method.
int x = 10;
{ int y = 5; x = x + y; }
System.out.println(x); // 15
// System.out.println(y); // error: y not in scopeLoops in Java: for, while, do-while, break, continue, Nested Loops and Scope: key facts
- Loop parts
- for (initialise; test; update)
- while (test)
- do { } while (test);
- Three loops.
- Number of iterations
- for i = a to b step 1: b - a + 1 passes | do-while: at least 1
- If b >= a.
- Scope
Local its block Loop variable the loop Instance the object - Visibility.
How to answer Loops in Java: for, while, do-while, break, continue, Nested Loops and Scope questions
Make a trace table with one row per pass.
- 1List the variables with starting values.
- 2Test the condition before each pass (after, for do-while).
- 3Apply break and continue exactly where they occur.
- 4Write the output in the order printed.
Loops in Java: for, while, do-while, break, continue, Nested Loops and 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.
do-while
Suppose a program asks for a PIN and must ask at least once, repeating until the PIN is correct.
Which loop suits this and why?
Try it first, then show the solutionSolution
- The question must be asked once before anything can be tested.
- A do-while runs the body first and tests afterwards.
Answer: do-while: it runs at least once.
Memory hook: Do first, ask later.
Tricks to remember Loops in Java: for, while, do-while, break, continue, Nested Loops and Scope
Short memory aids. Say each one aloud once, then use it on a question the same day.
Entry vs exit
for and while test first; do-while tests last.
Use it when: Loops.
Continue skips, break stops
continue goes to the next pass; break leaves the loop.
Use it when: Jumps.
Brace is the wall
A local variable dies at its closing brace.
Use it when: Scope.
Loops in Java: for, while, do-while, break, continue, Nested Loops and Scope: solved examples
Ordered from board level to HOTS. Try each one before you open the solution.
Example 1: do-while · 1 mark · MCQBoard level
The loop int i = 10; do { System.out.print(i + " "); i++; } while (i < 5); prints:
(a) nothing (b) 10 (c) 10 11 12 (d) goes on forever
Try it first, then show the solutionSolution
- The body runs once before the test; 11 < 5 is false.
Answer: (b) 10
Example 2: Trace the loop · 3 marks · Short answerBoard level
Write the output: int s = 0; for (int i = 1; i <= 4; i++) { s += i * i; System.out.print(s + " "); } (3 marks)
Try it first, then show the solutionSolution
- i = 1: s = 1; i = 2: s = 5; i = 3: s = 14; i = 4: s = 30.
Answer: 1 5 14 30
Example 3: break and continue · 3 marks · Short answerHarder board level
Give the output: for (int i = 1; i <= 10; i++) { if (i % 3 == 0) continue; if (i > 7) break; System.out.print(i + " "); } and count how many times the body prints. (3 marks)
Try it first, then show the solutionSolution
- i = 1, 2 print; i = 3 skipped; 4, 5 print; 6 skipped; 7 prints; i = 8: not a multiple of 3, but i > 7 so break.
- Printed values: 1 2 4 5 7 (five).
Answer: 1 2 4 5 7 (the body prints 5 times)
Example 4: Nested loops and patterns · 5 marks · Long answerHarder board level
Give the output and the number of times the inner statement executes: for (int i = 1; i <= 4; i++) { for (int j = i; j <= 4; j++) System.out.print(j + " "); System.out.println(); } (5 marks)
Try it first, then show the solutionSolution
- i = 1: j = 1..4: "1 2 3 4".
- i = 2: j = 2..4: "2 3 4".
- i = 3: "3 4". i = 4: "4".
- Inner executions = 4 + 3 + 2 + 1 = 10.
Answer: 1 2 3 4
2 3 4
3 4
4
The inner statement executes 10 times.
Example 5: Scope · 5 marks · Long answerHOTS / competency
Predict the result of each part and name the scope involved: (a) int x = 10; { int y = 5; x = x + y; } System.out.println(x + " " + y); (b) class T { int x = 5; void f() { int x = 10; System.out.println(x + " " + this.x); } } new T().f(); (c) for (int i = 0; i < 3; i++) { } System.out.println(i); (5 marks)
Try it first, then show the solutionSolution
- (a) y is out of scope after the block: compile-time error "cannot find symbol y"; x would be 15.
- (b) The local x hides the instance variable: prints 10 5.
- (c) i is declared in the for header and is not visible after the loop: compile-time error.
- Scope: (a) block-local; (b) local vs instance; (c) loop variable.
Answer: (a) compile-time error (y out of scope); (b) 10 5; (c) compile-time error (i not in scope).
Where students lose marks in Loops in Java: for, while, do-while, break, continue, Nested Loops and Scope
The slips examiners see most often, and what to write instead.
Mistake: Counting a skipped iteration as printed
Ignoring continue.
Wrong working
1 2 3 4 5 6 7
Right working
1 2 4 5 7
Do this: Apply continue first.
Usually costs: 2 marks.
Mistake: Assuming do-while does not run when the condition is false
Treating it like while.
Wrong working
No output
Right working
One pass
Do this: Body first, then test.
Usually costs: 1-2 marks.
Mistake: Using a block variable outside the block
Scope misunderstanding.
Wrong working
Using y after the block
Right working
Declare y before the block
Do this: Declare in the outer scope.
Usually costs: 1-2 marks.
Exam tips
- Draw a trace table for every loop.
- Write the number of iterations in nested loop questions.
- Mention the scope of each variable when asked about visibility.
Practice questions on Loops in Java: for, while, do-while, break, continue, Nested Loops and 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) noneHint
Exit-controlled.
Answer
(c) do-while
Q2 · 2 marks · Short answerBoard level
Write the output: int i = 1; while (i <= 20) { i = i * 2; System.out.print(i + " "); } (2 marks)
Hint
Doubling.
Answer
2 4 8 16 32 (when i is 16 the test 16 <= 20 is true, so 32 is printed and the loop then stops)
Q3 · 4 marks · Short answerHarder board level
Write a program fragment using a loop to print the first 6 terms of the series 2, 5, 10, 17, ... (n squared + 1), and the sum of the terms. (4 marks)
Hint
1 + 1, 4 + 1, 9 + 1.
Answer
int s = 0; for (int n = 1; n <= 6; n++) { int t = n * n + 1; s += t; System.out.print(t + " "); } System.out.println("Sum = " + s); Terms: 2 5 10 17 26 37; sum = 97.
Q4 · 4 marks · Short answerHarder board level
Give the output of: for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { if (j == i) continue; System.out.print(i * 10 + j + " "); } } and the number of values printed. (4 marks)
Hint
Skip j == i.
Answer
i = 1: 12 13; i = 2: 21 23; i = 3: 31 32. Output: 12 13 21 23 31 32 (6 values).
Q5 · 3 marks · Short answerHOTS / competency
A programmer writes: int count = 0; for (int i = 0; i < 10; i++) { int count = i; } and gets a compile-time error. Explain, correct it, and find count after the corrected loop that adds i to count. (3 marks)
Hint
Redeclaration.
Answer
count is already declared in the enclosing scope, so declaring it again in the inner block is an error. Use count += i; with no new declaration: count = 0 + 1 + ... + 9 = 45.
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.Entry-controlled loops?Show answer
for and while.
2.do-while runs at least?Show answer
Once.
3.continue does?Show answer
Skips to the next iteration.
4.Scope of a local variable?Show answer
Its block.
5.Nested loop inner runs?Show answer
For every outer pass.
Loops in Java: for, while, do-while, break, continue, Nested Loops and Scope: frequently asked questions
Can a for loop have no body?
Yes, with a semicolon.
What if the update is missing?
The loop may run forever.
Is a loop variable visible after the loop?
Not if declared in the header.
What is an infinite loop?
A loop that never ends.