Mastering Java Selection Statements: If, Else‑If, and Switch Explained
This tutorial walks through Java's selection structures—including if, if‑else, if‑else‑if, nested if, and switch—detailing their syntax, execution flow, example code, output results, and key differences, helping beginners write correct and portable conditional logic.
Selection Structures Overview
Java offers three categories of program flow‑control statements: sequential, selection, and loop. This note concentrates on the selection structures that enable conditional execution.
1. if statement
The if statement is a single‑branch selection structure. If the condition evaluates to true, the following statement (or block) is executed; otherwise it is skipped.
Syntax when a single statement follows the condition:
if (conditionExpression)
statement // executed if conditionExpression returns trueSyntax when a block of statements follows the condition:
if (conditionExpression) {
// one or more statements // executed if conditionExpression returns true
}Execution steps:
Evaluate the condition inside the parentheses.
If the result is true, run the associated statement or block.
If the result is false, skip the statement/block and continue with the next statement after the if.
Example (demonstrating the importance of braces):
int x = 10, y = 5;
if (x > y)
System.out.println("香蕉比苹果贵");
System.out.println("苹果比香蕉贵"); // not part of the if‑statementOutput:
苹果比香蕉贵2. if‑else (double‑branch) statement
The if‑else statement chooses between two alternative branches based on the condition.
Syntax with single statements:
if (conditionExpression)
statement1 // executed if true
else
statement2 // executed if falseSyntax with blocks (recommended):
if (conditionExpression) {
// statements for true case
} else {
// statements for false case
}Execution steps mirror the if steps, adding the false‑branch execution when the condition is false.
Example – compare two numbers and print the larger one:
public class NumComparison {
public static void main(String[] args) {
int x = 33, y = 55, max = 0;
if (x > y) {
max = x;
} else {
max = y;
}
System.out.println("最大值为 :" + max);
}
}Output:
最大值为:553. if‑else‑if (multi‑branch) statement
The if‑else‑if construct allows an arbitrary number of conditional branches.
General syntax:
if (condition1) {
// block1
} else if (condition2) {
// block2
} ... else {
// final block
}Execution proceeds by evaluating each condition in order until one returns true; its block runs and the chain stops. If none are true, the optional final else block runs.
Example – grade judgement based on a score:
public class GradeJudgement {
public static void main(String[] args) {
int score = 75;
if (score >= 80) {
System.out.println("您的成绩为 A");
} else if (score >= 70) {
System.out.println("您的成绩为 B");
} else if (score >= 60) {
System.out.println("您的成绩为 C");
} else {
System.out.println("您的成绩为 D");
}
}
}Output: 您的成绩为 B Execution flow diagram:
4. Nested if statements
if statements can be placed inside other if statements to form nested decision trees.
Typical nested syntax:
if (condition1) {
if (condition2) {
// block1
} else {
// block2
}
} else {
if (condition3) {
// block3
} else {
// block4
}
}Execution steps:
Evaluate condition1. If true, evaluate condition2; otherwise evaluate condition3.
Execute the block corresponding to the first true condition encountered.
After the selected block finishes, the whole nested structure ends.
Example – find the maximum of three integers:
public class NumComparison {
public static void main(String[] args) {
int x = 33, y = 44, z = -55, max = 0;
if (x > y) {
if (x > z) {
max = x;
} else {
max = z;
}
} else {
if (y > z) {
max = y;
} else {
max = z;
}
}
System.out.println("最大值是:" + max);
}
}Output:
最大值是:555. switch statement
The switch statement provides a multi‑branch selection based on the value of an expression.
Syntax:
switch (expression) {
case constant1:
// statements for case 1
break;
case constant2:
// statements for case 2
break;
...
default:
// statements if no case matches
}Execution steps:
Evaluate the expression and compare its value with each case constant in order.
If a match is found, execute the associated statements until a break (or the end of the switch) is reached.
If no case matches, execute the default block (if present).
Key points:
The expression type must match the case constant type (e.g., int, char, String, enum). break can be omitted; without it, execution falls through to subsequent cases.
Only one default is allowed, typically placed at the end.
Example – grade judgement using switch:
public class GradeJudgement {
public static void main(String[] args) {
char grade = 'D';
switch (grade) {
case 'A':
System.out.println("优秀");
break;
case 'B':
System.out.println("良好");
break;
case 'C':
System.out.println("及格");
break;
default:
System.out.println("不及格");
}
}
}Output:
不及格6. Differences between if‑else‑if and switch
Both handle multi‑branch decisions, but switch works only with discrete values (int, char, String, enum) and cannot handle boolean or range checks. if‑else‑if is more flexible for range or complex boolean expressions.
When the decision is based on fixed constant values, switch offers clearer logic and generally higher execution efficiency.
For interval or range checks, if‑else‑if is preferred.
Lisa Notes
Lisa's notes: musings on daily life, work, study, personal growth, and casual reflections.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
