Control Statements: Selection, Iteration (incl. For-Each) & Jump Statements
switch only accepted byte, short, int, char, and enum. Beginning with JDK 7, switch also supports String expressions.1. Topic Introduction
Normally Java executes statements strictly top-to-bottom. Control statements give your program intelligence—allowing it to make decisions (Selection), repeat tasks (Iteration), or alter loop/method flow non-linearly (Jump).
Selection (if, switch) is a fork in the road choosing left or right based on a sign. Iteration (while, do-while, for, for-each) is a roundabout looping you until a condition clears (while checks BEFORE entering; do-while lets you drive around AT LEAST ONCE before checking!). Jump statements (break, continue, return) are your exits: break exits the roundabout completely, continue skips straight to the next lap, and return leaves the building entirely.
if, if-else-if, switch), Iteration (while, do-while, for, enhanced for-each), and Jump (break [incl. labeled], continue, return).3. Essential Exam Definitions
if, switch)boolean condition (if) or equality match against constants (switch).for-each Loopfor (type var : collection) (introduced in Java 5) that sequentially traverses arrays and collections using a read-only iteration variable.breakgoto in Java (break labelName;) used to break directly out of nested outer loops or labeled blocks.4. Detailed Technical Explanation & Syntax
1. Selection Statements (if, if-else-if Ladder, switch)
In Java, if (condition) REQUIRES a strictly boolean expression—if (x) where x is an int fails to compile! In nested if statements, an else always pairs with the nearest unmatched if in the same block.
Switch Rules: (1) switch tests ONLY equality against compile-time constants (case), whereas if tests arbitrary boolean ranges/relations; (2) No two case constants in the same switch can duplicate values; (3) Omitting break; triggers fall-through into the next case; (4) Supports byte, short, int, char, enum, and (since JDK 7) String.
2. Iteration Statements (while, do-while, for, for-each)
while (entry-controlled): Checks condition BEFORE each iteration (runs 0 or more times). do-while (exit-controlled): Executes body first, checks condition at bottom with trailing semicolon (while(cond);) — ideal for menu-driven programs (runs at least once).
for (init; cond; iter) executes init once, checks cond, executes body, then runs iter. Supports multiple comma-separated variables (for(a=1, b=4; a<=b; a++, b--)) or empty expressions (for(;;) infinite loop).
Enhanced for-each: for (type itr : array). Crucial exam rule: itr is read-only with respect to the underlying array—modifying itr = 99; inside the loop does NOT alter the array!
3. Jump Statements (break, continue, return)
break has three uses: (1) terminates a switch case; (2) exits the innermost enclosing loop immediately; (3) labeled break outer; exits outer nested blocks/loops.
continue skips the remaining body of the current iteration and jumps to the condition check (while/do-while) or the iteration step (for). Supports labels (continue outer;).
return exits the current method immediately and transfers control (and optional value) back to the caller.
5. Working Principle: Execution Difference: continue in while vs for
- In a
for (int i = 0; i < 5; i++)loop,continue;jumps toi++FIRST and then checksi < 5. - In a
while (i < 5)loop,continue;jumps straight toi < 5— ifi++came aftercontinue, it creates an infinite loop!
7. Complete Java Code Examples & Tracing
| 1 | public class ControlStatementsDemo { |
| 2 | public static void main(String[] args) { |
| 3 | // 1. String switch (JDK 7+) |
| 4 | String grade = "A"; |
| 5 | switch (grade) { |
| 6 | case "S": |
| 7 | System.out.println("Outstanding"); |
| 8 | break; |
| 9 | case "A": |
| 10 | System.out.println("Excellent"); |
| 11 | break; |
| 12 | default: |
| 13 | System.out.println("Passed"); |
| 14 | } |
| 15 | |
| 16 | // 2. Labeled break in nested loop |
| 17 | outer: for (int i = 1; i <= 3; i++) { |
| 18 | for (int j = 1; j <= 3; j++) { |
| 19 | if (i == 2 && j == 2) { |
| 20 | System.out.println("Breaking outer at i=2, j=2"); |
| 21 | break outer; |
| 22 | } |
| 23 | System.out.print("(" + i + "," + j + ") "); |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | // 3. Enhanced for-each read-only behavior |
| 28 | int[] nums = {10, 20, 30}; |
| 29 | for (int n : nums) { |
| 30 | n = n * 2; // Modifies local copy 'n', NOT nums[]! |
| 31 | } |
| 32 | System.out.println("nums[0] after for-each = " + nums[0]); |
| 33 | } |
| 34 | } |
Excellent (1,1) (1,2) (1,3) (2,1) Breaking outer at i=2, j=2 nums[0] after for-each = 10
ifelseswitchcasebreakcontinuewhiledoforreturn- Forgetting the semicolon at the end of
do { ... } while (cond);. - Using
if (count)with an integer variable instead ofif (count != 0). - Trying to modify array elements directly using an enhanced
for-eachloop variable.
Comparison Tables
while vs do-while| Criteria | while Loop | do-while Loop |
|---|---|---|
| Control Type | Entry-controlled (condition checked first) | Exit-controlled (condition checked last) |
| Minimum Executions | 0 times (if condition initially false) | 1 time (body always executes once) |
| Semicolon | No semicolon after while (cond) | Mandatory ; after while (cond); |
Real-World Applications
do-whilepowers CLI menus;switchdispatches command routers;for-eachcleanly processes read-only arrays.
Important KTU Exam Points
- State all 3 uses of
break: exitswitch, exit innermost loop, and labeledbreak(civilized goto). - Know the exact Java versions:
switchgainedStringsupport in JDK 7.
Memory Techniques & Revision
- Control Trio: S-I-J (Selection
if/switch, Iterationfor/while/do-while, Jumpbreak/continue/return).
Common Pitfalls to Avoid
- Using
for-eachwhen you need the indexior want to overwrite primitive elements in an array.
KTU-Style Questions & Exam-Ready Answers
Structured specifically for KTU Part A (3-mark) and Part B (6/9-mark) evaluation rubrics.
- Syntax
for (type var : collection)sequentially traverses arrays/collections from start to end without an explicit counter. - Limitation: The iteration variable
varis read-only for primitive arrays; assigningvar = 0;does not alter the underlying array element.
| int[] a = {1, 2, 3}; |
| for (int x : a) { x = 99; } // a[0] remains 1 |
Quick Understanding Check
1. Which statement is true regarding switch in Java?