Unit 3: Java Programming
Chapter 3: Control Structures - Conditionals and Loops
1. Flow of Control
By default, statements in a Java program execute sequentially from top to bottom. Control structures alter this sequential flow:
1. Selection (Decision Making): if, if-else, nested if, switch.
2. Iteration (Looping): for, while, do-while.
3. Jump Statements: break, continue, return.
2. Decision Making Statements
1. if-else and if-else-if Ladder
int marks = 85;
if (marks >= 90) {
System.out.println("Grade: A+");
} else if (marks >= 80) {
System.out.println("Grade: A");
} else if (marks >= 70) {
System.out.println("Grade: B");
} else {
System.out.println("Grade: C");
}
2. The switch Statement
Selects one of many code blocks to be executed based on matching an expression value.
- Expression can be: byte, short, int, char, String, or enum.
- break prevents "fall-through" into subsequent cases.
- default executes if no case matches.
int day = 3;
String dayName;
switch (day) {
case 1: dayName = "Monday"; break;
case 2: dayName = "Tuesday"; break;
case 3: dayName = "Wednesday"; break;
case 4: dayName = "Thursday"; break;
case 5: dayName = "Friday"; break;
case 6: dayName = "Saturday"; break;
case 7: dayName = "Sunday"; break;
default: dayName = "Invalid Day Number"; break;
}
System.out.println(dayName);
3. Iteration / Looping Statements
1. The for Loop (Definite Loop)
Ideal when the number of iterations is known beforehand.
// Syntax: for (initialization; condition; update) { ... }
for (int i = 1; i <= 5; i++) {
System.out.print(i + " ");
}
// Output: 1 2 3 4 5
2. The while Loop (Entry-Controlled Loop)
Condition is tested before entering the loop body. If condition is false initially, the loop body never executes.
int count = 1;
while (count <= 5) {
System.out.print(count + " ");
count++;
}
3. The do-while Loop (Exit-Controlled Loop)
Condition is evaluated after the loop body executes. - Key Guarantee: Guaranteed to execute at least once, even if the condition is false from the start.
int num = 10;
do {
System.out.println("Executed at least once!");
} while (num < 5);
Loop Comparison Table
| Feature | for Loop |
while Loop |
do-while Loop |
|---|---|---|---|
| Control Point | Entry-controlled | Entry-controlled | Exit-controlled |
| Min. Iterations | 0 | 0 | 1 |
| Ideal Scenario | Known number of iterations | Indefinite condition checking | Interactive menu-driven programs |
4. Jump Statements: break vs. continue
| Feature | break Statement |
continue Statement |
|---|---|---|
| Effect | Terminates loop entirely and jumps out | Skips current iteration and moves to next |
| Used In | Loops and switch statements |
Only inside loops |
// Example demonstrating break and continue
for (int i = 1; i <= 5; i++) {
if (i == 3) continue; // skips 3
if (i == 5) break; // terminates before printing 5
System.out.print(i + " ");
}
// Output: 1 2 4
5. Board Exam Output Questions
- What will be the output of the following Java snippet?
java int x = 2; while (x < 10) { x += 3; } System.out.println(x); - Trace:
- Start: $x = 2$ ($2 < 10 o x = 5$)
- Iteration 2: $x = 5$ ($5 < 10 o x = 8$)
- Iteration 3: $x = 8$ ($8 < 10 o x = 11$)
- Iteration 4: $11 < 10$ is False $ o$ Loop terminates.
- Output:
11