KTU OOP Java2024 Scheme • 60M ESE
MODULE 1/Section 1: Introduction to Java

Control Statements: Selection, Iteration (incl. For-Each) & Jump Statements

MEDIUM PRIORITY (Prep Guide)Official Syllabus~15 min study
Official KTU 2024 Syllabus Topics Covered Here
Control statements: Selection statements, Iteration statements, Jump statements
Java Version Note: Prior to JDK 7, 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).

2. Start From Zero: Traffic Forks, Roundabouts, and Emergency Exits

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.

Key Takeaway: 3 categories: Selection (if, if-else-if, switch), Iteration (while, do-while, for, enhanced for-each), and Jump (break [incl. labeled], continue, return).

3. Essential Exam Definitions

Selection Statements (if, switch)
Control structures that route execution along distinct branches depending on a boolean condition (if) or equality match against constants (switch).
Enhanced for-each Loop
Iteration syntax for (type var : collection) (introduced in Java 5) that sequentially traverses arrays and collections using a read-only iteration variable.
Labeled break
A civilized replacement for goto 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

  1. In a for (int i = 0; i < 5; i++) loop, continue; jumps to i++ FIRST and then checks i < 5.
  2. In a while (i < 5) loop, continue; jumps straight to i < 5 — if i++ came after continue, it creates an infinite loop!

7. Complete Java Code Examples & Tracing

Switch (with Fall-Through), Labeled Break, Continue & For-Each Read-Only RuleControlStatementsDemo.java
Combines String switch, do-while menu check, labeled break out of nested loops, and proves for-each does not mutate primitive array items.
1public 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}
Expected Output
Excellent
(1,1) (1,2) (1,3) (2,1) Breaking outer at i=2, j=2
nums[0] after for-each = 10
Keywords:ifelseswitchcasebreakcontinuewhiledoforreturn
Common Exam & Coding Mistakes
  • Forgetting the semicolon at the end of do { ... } while (cond);.
  • Using if (count) with an integer variable instead of if (count != 0).
  • Trying to modify array elements directly using an enhanced for-each loop variable.

Comparison Tables

while vs do-while
Criteriawhile Loopdo-while Loop
Control TypeEntry-controlled (condition checked first)Exit-controlled (condition checked last)
Minimum Executions0 times (if condition initially false)1 time (body always executes once)
SemicolonNo semicolon after while (cond)Mandatory ; after while (cond);

Real-World Applications

  • do-while powers CLI menus; switch dispatches command routers; for-each cleanly processes read-only arrays.

Important KTU Exam Points

  • State all 3 uses of break: exit switch, exit innermost loop, and labeled break (civilized goto).
  • Know the exact Java versions: switch gained String support in JDK 7.

Memory Techniques & Revision

  • Control Trio: S-I-J (Selection if/switch, Iteration for/while/do-while, Jump break/continue/return).

Common Pitfalls to Avoid

  • Using for-each when you need the index i or 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 var is read-only for primitive arrays; assigning var = 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?