Java Switch Case Practice Sheet for Beginners
What is a switch Statement?
The switch statement is used to choose between multiple options based on the value of a variable.
Syntax:
switch (expression) {
case value1:
// Code to execute
break;
case value2:
// Code to execute
break;
default:
// Code to execute if no case matches
Practice Problems
Basic Level:
1. Day of the Week:
Write a program that prints the day name (e.g., Monday, Tuesday) based on number input (1-7).
2. Grade Checker:
Input a character ('A', 'B', 'C', 'D', 'F') and print:
A => Excellent
B => Good
C => Average
D => Poor
F => Fail
3. Simple Calculator:
Input two numbers and a character for the operator ('+', '-', '*', '/') and perform the operation.
Java Switch Case Practice Sheet for Beginners
Intermediate Level:
4. Month Days:
Input a number (1-12) and print the number of days in that month (non-leap year).
5. Vowel or Consonant:
Input a lowercase alphabet and determine if it is a vowel or consonant using switch.
6. Traffic Light System:
Input color name ('red', 'yellow', 'green') and display the action (Stop, Ready, Go).
Bonus: Small Program Tasks:
7. Menu-Driven Calculator:
Display a menu:
1. Add
2. Subtract
3. Multiply
4. Divide
Ask user to choose and input two numbers, perform operation using switch.
8. Mini ATM Simulator:
Create a switch menu:
1. Check Balance
2. Deposit
3. Withdraw
4. Exit
Simulate balance operations.
Tips
- Always include a 'default' case.
- Use 'break' to prevent fall-through.
- switch supports: int, byte, short, char, String (Java 7+), and enums.