Switch Statements
Use switch to select among many options. Learn case labels, break, default, valid types, fall-through, and modern arrow switch.
Switch Statements is a free Java Academy lesson on CoddyKit — lesson 2 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Switch
Why switch? When many if/else branches test the same variable, switch is clearer and faster to scan.
Classic Syntax
Classic syntax
Use break to stop after a case. default runs when no case matches.
public class Main {
public static void main(String[] args) {
int n = 2;
switch (n) {
case 1:
System.out.println("one");
break;
case 2:
System.out.println("two");
break;
default:
System.out.println("other");
}
}
}
Valid Types
Valid types for classic switch: byte, short, char, int, String, and enum types.
Case labels must be constants (e.g., literals or enum values).
Fall-through
Fall-through happens when a case has no break.
int n = 1;
switch (n) {
case 1:
case 2:
System.out.println("one or two");
break;
}
Stack cases to handle multiple values the same way.
Arrow Switch
Arrow switch (modern Java) is concise and avoids fall-through.
public class Main {
public static void main(String[] args) {
String grade = "B";
String msg = switch (grade) {
case "A" -> "Excellent";
case "B", "C" -> "Passed";
default -> "Keep trying";
};
System.out.println(msg);
}
}
Switch Demo
Try it: Enter a number 1–7. See stacked cases and break in action.
public class Main {
public static void main(String[] args) {
// Instead of Scanner input, we assign a fixed value
int day = 6; // try changing this value (1–7)
switch (day) {
case 1:
case 2:
case 3:
case 4:
case 5:
System.out.println("Weekday");
break;
case 6:
case 7:
System.out.println("Weekend");
break;
default:
System.out.println("Invalid day");
}
}
}
Prevent Fall-through
Quick check: Which keyword prevents fall-through to the next case in a classic switch?
Recap & Next
Recap: You learned switch syntax, valid types, fall-through, default, and the modern arrow form.
Next: Build a small console calculator using conditions and switch.
Frequently asked questions
Is the “Switch Statements” lesson free?
Yes — the full text of “Switch Statements” is free to read here on the web, and the Java Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.
What will I learn in “Switch Statements”?
Use switch to select among many options. Learn case labels, break, default, valid types, fall-through, and modern arrow switch. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Java Academy?
No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “Switch Statements” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Java Academy lesson?
Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.