Control Flow
## Learning Objectives
- Master if, else if, else statements
- Understand switch statements
- Learn for, while, do-while loops
- Use break and continue
## if Statement
### Basic if
```java
int age = 18;
if (age >= 18) {
System.out.println("Adult");
}
```
### if-else
```java
int age = 15;
if (age >= 18) {
System.out.println("Adult");
} else {
System.out.println("Minor");
}
```
### if-else if-else
```java
int score = 85;
char grade;
if (score >= 90) {
grade = 'A';
} else if (score >= 80) {
grade = 'B';
} else if (score >= 70) {
grade = 'C';
} else if (score >= 60) {
grade = 'D';
} else {
grade = 'F';
}
System.out.println("Grade: " + grade);
```
## Ternary Operator
```java
int age = 20;
String status = (age >= 18) ? "adult" : "minor";
```
## switch Statement
### Basic switch
```java
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";
}
System.out.println(dayName);
```
### Switch with Arrow (Java 14+)
```java
String dayName = switch (day) {
case 1 -> "Monday";
case 2 -> "Tuesday";
case 3 -> "Wednesday";
case 4 -> "Thursday";
case 5 -> "Friday";
case 6 -> "Saturday";
case 7 -> "Sunday";
default -> "Invalid";
};
```
### Multiple Cases
```java
switch (month) {
case 1, 3, 5, 7, 8, 10, 12:
days = 31;
break;
case 4, 6, 9, 11:
days = 30;
break;
case 2:
days = 28;
break;
}
```
## for Loop
### Basic for
```java
for (int i = 0; i < 5; i++) {
System.out.println(i); // 0, 1, 2, 3, 4
}
```
### Enhanced for (for-each)
```java
int[] numbers = {1, 2, 3, 4, 5};
for (int num : numbers) {
System.out.println(num);
}
```
### With Multiple Variables
```java
for (int i = 0, j = 10; i < 5; i++, j--) {
System.out.println(i + " - " + j);
}
```
## while Loop
### Basic while
```java
int i = 0;
while (i < 5) {
System.out.println(i);
i++;
}
```
### while with Condition First
```java
Scanner scanner = new Scanner(System.in);
String input;
while (scanner.hasNext()) {
input = scanner.next();
if (input.equals("quit")) {
break;
}
System.out.println("You entered: " + input);
}
```
## do-while Loop
### Executes at Least Once
```java
int i = 0;
do {
System.out.println(i);
i++;
} while (i < 5);
```
### Menu Example
```java
Scanner scanner = new Scanner(System.in);
int choice;
do {
System.out.println("1. New Game");
System.out.println("2. Load Game");
System.out.println("3. Quit");
System.out.print("Choose: ");
choice = scanner.nextInt();
} while (choice < 1 || choice > 3);
```
## break and continue
### break - Exit Loop
```java
for (int i = 0; i < 10; i++) {
if (i == 5) {
break; // Exit when i is 5
}
System.out.println(i); // 0, 1, 2, 3, 4
}
```
### continue - Skip Iteration
```java
for (int i = 0; i < 5; i++) {
if (i == 2) {
continue; // Skip when i is 2
}
System.out.println(i); // 0, 1, 3, 4
}
```
## Nested Loops
```java
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.printf("(%d,%d) ", i, j);
}
System.out.println();
}
// Output:
// (0,0) (0,1) (0,2)
// (1,0) (1,1) (1,2)
// (2,0) (2,1) (2,2)
```
## Labels
### Named Loops
```java
outer:
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (j == 2) {
break outer; // Breaks outer loop
}
System.out.println(i + "," + j);
}
}
```
## Common Patterns
### Sum 1 to N
```java
int sum = 0;
for (int i = 1; i <= 100; i++) {
sum += i;
}
```
### Find in Array
```java
int[] numbers = {3, 7, 2, 9, 4};
int target = 9;
for (int num : numbers) {
if (num == target) {
System.out.println("Found!");
break;
}
}
```
### Count Matches
```java
int[] numbers = {1, 2, 3, 2, 4, 2, 5};
int target = 2;
int count = 0;
for (int num : numbers) {
if (num == target) {
count++;
}
}
System.out.println("Count: " + count);
```
## Summary
- if/else if/else for conditional logic
- switch for multiple discrete values
- for loop for known iterations
- while loop for condition-based iteration
- do-while for at-least-once iteration
- break exits loop; continue skips iteration
- Use labels for nested loop control
Comments
Comments powered by Giscus
To enable comments, add your Giscus embed code here.
Learn more about Giscus →