Unit3 Home | Unit3 3.1 | Unit3 3.2 | Unit3 3.3 | Unit3 3.4 | Unit3 3.5 | Unit3 3.6 | Unit3 3.7 | Unit3 HW |
Unit 3 Team Teach - 3.4
Unit 3 Team Teach
3.4 Else If Statements
Else If Statements: Used when you have multiple conditions that need to be checked sequentially.
Flow of Execution: Each condition is evaluated in the order written. The first true condition’s code runs, and the rest are skipped.
Structure:
- Start with a single if statement.
- Follow with as many else if statements as needed.
- Optionally end with one else to handle any remaining cases. Key Concept: The order of conditions matters. More specific conditions should come before broader ones to ensure accurate results.
- If I was 19 what would it print out?
- If I was 13 what would it print out?
- Create your if statement with one else if condition.
answer
- You can register to vote. You are old enough for a license to drive.
- no output since the min is 14.
- ↓↓↓↓↓
int myAge = 13;
System.out.println("Current age: " + myAge);
if (myAge >= 16) {
System.out.println("You are old enough for a license to drive.");
} else if (myAge >= 14) {
System.out.println("You are a teenager.");
} else {
System.out.println("You are not yet a teenager.");
}
Current age: 13
You are not yet a teenager.