Loops repeat a block of statements. Jump statements change the normal flow inside loops.
The while and do-while Loops
The while loop is an entry-controlled loop: it tests the condition before each pass, so the body may run zero times. while (i <= 10) { ...; i++; }. The do-while loop is exit-controlled: it tests the condition after each pass, so the body runs at least once. do { ... } while (condition);. It suits menus, where the menu must be shown at least once.
Definition: Loop. A control structure that executes a block of statements repeatedly as long as a given condition remains true.
The for Loop
The for loop puts initialisation, condition and update in one line: for (int i = 1; i <= 10; i++) { ... }. It is used when the number of repetitions is known. Several variables may be used, separated by commas, and any part may be left empty; for (;;) is an infinite loop. The enhanced for (for-each) loop runs through every element of an array or collection: for (int x : arr) { ... }. Loops can be nested, as in printing patterns or processing matrices.
Jumps in Loops
The break statement ends the nearest enclosing loop or switch at once. The continue statement skips the rest of the current pass and goes to the next pass of the loop. The return statement ends the method.
Labelled Loops
Java has no goto, but it allows labels on loops. A label is an identifier followed by a colon, placed before a loop: outer: for (...). break outer; ends the labelled outer loop, not just the inner one, and continue outer; goes to the next pass of the outer loop. Labelled loops are useful for leaving nested loops, such as when a search in a matrix finds its target.
Table 2.6: Loops in Java
| Loop | Test | Minimum passes |
|---|---|---|
| while | Before each pass | 0 |
| do-while | After each pass | 1 |
| for | Before each pass | 0 |
| for-each | Over each element | 0 (empty array) |
Example 2.6: Sum, skip and labelled break
Problem: (a) Sum the numbers 1 to 10 skipping multiples of 3. (b) Find the first pair (i, j) with i × j = 12, for i and j from 1 to 6, and leave both loops at once.
Solution:
- Sum 1 to 10 is 55; the multiples of 3 are 3, 6 and 9, summing 18. So the sum is 37.
- Search i from 1: for i = 2, j = 6 gives 12. break outer leaves both loops.
Result: (a) 37, (b) the pair (2, 6).
public class Loops {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 10; i++) {
if (i % 3 == 0) continue;
sum += i;
}
System.out.println("sum without multiples of 3 = " + sum);
outer:
for (int i = 1; i <= 6; i++)
for (int j = 1; j <= 6; j++)
if (i * j == 12) {
System.out.println("first pair: " + i + " x " +
j);
break outer;
}
int n = 0;
do { n++; } while (n < 0);
System.out.println("do-while ran " + n + " time(s)");
int[] marks = {70, 85, 60};
for (int m : marks) System.out.print(m + " ");
System.out.println();
}
}
Output
sum without multiples of 3 = 37
first pair: 2 x 6
do-while ran 1 time(s)
70 85 60
Common mistakes
- Forgetting to update the loop variable, causing an infinite loop.
- Placing a semicolon after for (...);, which makes an empty loop body.
- Expecting break to leave nested loops without a label.
Key takeaways
- while and for test first; do-while tests last and runs at least once.
- for-each visits every element of an array.
- break ends a loop; continue skips to the next pass.
- Labelled break and continue control outer loops.
Practice questions
- Distinguish while and do-while.
- What does continue do?
- What is a labelled break?
Answers to the practice questions
- while tests before each pass and may not run at all; do-while tests after and runs at least once.
- It skips the rest of the current pass and starts the next pass of the loop.
- A break with a label that ends the labelled outer loop, not just the innermost one.
Sources
- Official BCA syllabus Object Oriented Programming using Java, course code 25BCA402SEC01, Unit II
