Learn how to use switch statements for multiple conditions.
रोजच्या जीवनातील उदाहरणे (Real Life Examples in Marathi)
1. आठवड्याचे दिवस आणि काय करायचे?
let आजचादिवस = "सोमवार";
switch(आजचादिवस) {
case "सोमवार":
console.log("शाळेला जायचे 🏫");
break;
case "रविवार":
console.log("सुट्टी आहे! 🎮");
break;
case "शनिवार":
console.log("अर्धा दिवस शाळा 📚");
break;
default:
console.log("नेहमीप्रमाणे शाळा 📝");
}
2. खाऊ काय करायचा?
let वार = "गुरुवार";
switch(वार) {
case "सोमवार":
console.log("पोहे 🍚");
break;
case "गुरुवार":
console.log("पुरणपोळी 🥮");
break;
case "रविवार":
console.log("बिर्याणी 🍛");
break;
default:
console.log("दाल-भात 🍚");
}
1. Simple Switch
let day = 1;
switch(day) {
case 1:
console.log("Monday");
break;
case 2:
console.log("Tuesday");
break;
default:
console.log("Another day");
}
Result: Select a day
2. Grouped Cases
let month = "Jan";
switch(month) {
case "Dec":
case "Jan":
case "Feb":
console.log("Winter");
break;
case "Mar":
case "Apr":
case "May":
console.log("Spring");
break;
default:
console.log("Another season");
}
Result: Select a month
3. Grade Calculator
let grade = "A";
switch(grade) {
case "A":
console.log("Excellent!");
break;
case "B":
console.log("Good job!");
break;
case "C":
console.log("Passed");
break;
default:
console.log("Try again");
}
Result: Select a grade
4. Traffic Light
let light = "red";
switch(light) {
case "red":
console.log("Stop!");
break;
case "yellow":
console.log("Caution!");
break;
case "green":
console.log("Go!");
break;
default:
console.log("Invalid light");
}
Result: Select a light
Switch Statement Syntax
Basic Switch Statement Structure
switch(expression) {
case value1:
// code to execute
break;
case value2:
// code to execute
break;
default:
// code to execute if no case matches
}
Key Points:
The switch expression is evaluated once
Each case value is compared with the expression
If there's a match, the code block is executed
break statements prevent fall-through to next case
default case runs if no case matches
Tips and Best Practices
Always use break statements unless fall-through is intended
Group related cases together when they share the same code
Use default case to handle unexpected values
Switch statements can be cleaner than multiple if-else statements
Practice Exercises
Exercise 1: Day Type Checker
Write a switch statement that takes a day (Monday to Sunday) and outputs whether it's a:
Weekday (Monday to Friday)
Weekend (Saturday and Sunday)
// Your solution here
switch(day) {
case "Monday":
case "Tuesday":
case "Wednesday":
case "Thursday":
case "Friday":
console.log("Weekday");
break;
case "Saturday":
case "Sunday":
console.log("Weekend");
break;
default:
console.log("Invalid day");
}
Exercise 2: Fruit Categories
Create a switch statement that categorizes fruits into:
Citrus (orange, lemon, lime)
Tropical (mango, pineapple, banana)
Berry (strawberry, blueberry, raspberry)
// Try writing your solution here
switch(fruit) {
case "orange":
case "lemon":
case "lime":
console.log("Citrus fruit");
break;
case "mango":
case "pineapple":
case "banana":
console.log("Tropical fruit");
break;
case "strawberry":
case "blueberry":
case "raspberry":
console.log("Berry");
break;
default:
console.log("Unknown fruit type");
}