3.1 Selection: if, if-else, else-if Ladder
if (marks >= 90) grade = 'A';
else if (marks >= 75) grade = 'B';
else if (marks >= 60) grade = 'C';
else grade = 'F';
The condition must be boolean — if (x = 5) is a compile error in Java (unlike C, where it is a silent bug). The dangling else binds to the nearest unmatched if, so always use braces.
3.2 switch — Classic vs Arrow Form
Classic switch works on int-compatible types (byte, short, char, int), String (since Java 7), enums, and wrapper types — not long, float, double, or boolean.
switch (day) {
case 1:
case 7: System.out.println("Weekend"); break;
case 2: System.out.println("Monday"); break; // forget break => fall-through!
default: System.out.println("Weekday");
}
Fall-through is the number-one trap: without break, execution continues into the next case, even default.
Arrow switch (Java 14+) eliminates fall-through and can be an expression that yields a value:
String type = switch (day) {
case 1, 7 -> "Weekend";
case 2, 3, 4, 5, 6 -> "Weekday";
default -> {
System.out.println("Invalid input");
yield "Unknown"; // yield returns a value from a block
}
};
| Feature | Classic switch | Arrow switch |
|---|---|---|
| Fall-through | Yes (needs break) | Never |
| Multiple labels per case | Via stacked cases | case 1, 7 -> |
| Usable as expression | No | Yes (yield in blocks) |
| Exhaustiveness checked | No | Yes, for expressions |
3.3 Iteration: while, do-while, for, for-each
| Loop | Test position | Minimum executions | Best for |
|---|---|---|---|
while | Entry (top) | 0 | Unknown iteration count |
do-while | Exit (bottom) | 1 | Menus, "ask again" input |
for | Entry | 0 | Known count / counters |
| for-each | — | 0 | Traversing arrays/collections |
for (int i = 1; i <= 3; i++) System.out.print(i + " "); // 1 2 3
int[] arr = {10, 20, 30};
for (int v : arr) System.out.print(v + " "); // 10 20 30
for-each limitations (exam favourite): no index available, cannot modify the array slot through the loop variable, traverses forward only. All three sections of a classic for are optional — for(;;) is an infinite loop.
3.4 Jump Statements: break, continue, Labels
breakexits the innermost loop or switch.continueskips to the next iteration.- A label lets you break/continue an outer loop — the only sanctioned "goto-like" jump in Java:
outer:
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
if (i * j == 4) break outer; // leaves BOTH loops
System.out.println(i + "," + j);
}
}
Output:
1,1
1,2
1,3
2,1
3.5 Classic Loop Patterns (Write-a-Program Bank)
Reverse digits / palindrome number:
int n = 121, rev = 0, t = n;
while (t != 0) { rev = rev * 10 + t % 10; t /= 10; }
System.out.println(n == rev ? "Palindrome" : "Not palindrome");
Prime check (trial division up to √n):
boolean prime = n > 1;
for (int i = 2; i * i <= n; i++)
if (n % i == 0) { prime = false; break; }
Pyramid pattern (nested loops — printed in the code snippet below), Fibonacci series, Armstrong numbers, and factorial round out the standard exam bank. Master the pattern skeleton: outer loop = rows, inner loop(s) = spaces then symbols.
🎯 Exam Focus
- Compare
whileanddo-whileloops. Write a menu-driven program usingdo-whileandswitch. - What is fall-through in switch? How does the arrow (
->) form of switch eliminate it? Which data types are NOT allowed in a switch expression? - Explain labelled
breakandcontinuewith a nested-loop example and its exact output. - Write a program to check whether a number is prime, and another to print all Armstrong numbers between 1 and 1000.
- Write a program to print a pyramid of stars of height n using nested loops.
- Predict the output of a switch with missing
breakstatements (fall-through trace).