Switch Statement
Part of the Fundamentals section of Coddy's Java journey — lesson 29 of 73.
The switch statement is like a multi-way if statement. Instead of evaluating a single condition, it checks the value of a variable against multiple cases and executes the code associated with the matching case.
Here's the basic structure of a switch statement:
switch (variable) {
case value1:
// Code to execute if variable equals value1
break;
case value2:
// Code to execute if variable equals value2
break;
// ... more cases
default:
// Code to execute if no case matches
}- The
switchkeyword is followed by the variable you want to test in parentheses. - Each
caserepresents a possible value of the variable. - The code inside each
caseis executed if the variable matches that case's value.
- The
breakstatement is crucial; it exits theswitchafter a case is executed. Without it, execution would "fall through" to the next case. - The
defaultcase is optional and is executed if no other case matches.
Here's an example:
int day = 3;
String dayName;
switch (day) {
case 1:
dayName = "Monday";
break;
case 2:
dayName = "Tuesday";
break;
// ... cases for other days
default:
dayName = "Invalid day";
}You can also combine multiple cases into one:
int day = 3;
String dayName;
switch (day) {
case 1:
case 2:
case 3:
dayName = "Start of week";
break;
// ... cases for other days
default:
dayName = "Invalid day";
}Challenge
BeginnerCreate a program that takes a month number (1 for January, 2 for February, etc.) and prints the season it belongs to. Use a switch statement for the logic.
The seasons and their corresponding months are:
- Winter: December (12), January (1), February (2)
- Spring: March (3), April (4), May (5)
- Summer: June (6), July (7), August (8)
- Autumn: September (9), October (10), November (11)
For any other month number, print "Invalid month".
Cheat sheet
The switch statement is a multi-way conditional that checks a variable against multiple cases:
switch (variable) {
case value1:
// Code to execute if variable equals value1
break;
case value2:
// Code to execute if variable equals value2
break;
default:
// Code to execute if no case matches
}Key components:
case: represents a possible value of the variablebreak: exits the switch after a case is executed (prevents fall-through)default: optional case executed if no other case matches
You can combine multiple cases:
switch (day) {
case 1:
case 2:
case 3:
dayName = "Start of week";
break;
default:
dayName = "Invalid day";
}Try it yourself
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int month = scanner.nextInt();
String season = "";
// Write your code below
System.out.println(season);
}
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsModulo OperatorIncrement/DecrementPost Increment/DecrementArithmetic ShortcutsComparison OperatorsString Comparison5Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Logical Operators Part 43Variables Part 2
ConstantsNaming ConventionsRecap - Initialize VariablesType Casting Part 1Type Casting Part 26Decision Making
If StatementIf - ElseSwitch StatementTernary OperatorRecap - If ElseNested If - Else