Java का switch Statement
In this page:
switch (expression) {
case value1:
// code
break;
default:
// code
}
Basic switch Statement
एक switch statement एक variable की value को sequence में possible cases की एक list से compare करता है, जो उसी variable को test करने वाले else-if blocks की एक लंबी chain से ज़्यादा साफ़ पढ़ता है।
उदाहरण: Basic switch Statement
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Declare day and set it to 3
int day = 3;
// Choose a branch based on the value of day
switch (day) {
case 1: System.out.println("Monday"); break;
case 2: System.out.println("Tuesday"); break;
case 3: System.out.println("Wednesday"); break;
}
}
}
Login to try C/C++/Java/PHP code in the editor
The Importance of break
एक matching case के आखिर में एक break के बिना, execution अगले case के code में 'fall through' हो जाता है भले ही इसकी अपनी condition न जांची गई हो -- यह switch statements में नए developers के लिए bugs का एक आम source है।
उदाहरण: The Importance of break
public class Main {
public static void main(String[] args) {
int day = 1;
switch (day) {
case 1: System.out.println("Monday");
case 2: System.out.println("Tuesday"); break; // falls through from case 1
case 3: System.out.println("Wednesday"); break;
}
}
}
Login to try C/C++/Java/PHP code in the editor
Using the default Case
default else का switch equivalent है, सिर्फ तब चलता है जब कोई explicit case value switch के expression से match न हो -- unexpected values को gracefully handle करने के लिए हमेशा एक शामिल करना अच्छा practice है।
उदाहरण: Using the default Case
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Declare day and set it to 9
int day = 9;
// Choose a branch based on the value of day
switch (day) {
case 1: System.out.println("Monday"); break;
case 2: System.out.println("Tuesday"); break;
// default runs when no case matches
default: System.out.println("Unknown day"); break;
}
}
}
Login to try C/C++/Java/PHP code in the editor
Grouping Switch Cases
कई case labels को बीच में कोई code न रखते हुए एक-दूसरे के पीछे stack करना, जैसे case 1: case 2: doSomething(); break;, 1 और 2 दोनों को वही shared block trigger कराता है बजाय उस code को दो बार duplicate करने के।
उदाहरण: Grouping Switch Cases
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Declare num and set it to 2
int num = 2;
// Choose a branch based on the value of num
switch (num) {
case 1:
case 2:
System.out.println("One or two");
// Leave the switch/loop
break;
// default runs when no case matches
default:
System.out.println("Something else");
}
}
}
Login to try C/C++/Java/PHP code in the editor
Switch on String
Java 7 से, आप सीधे एक String value पर switch कर सकते हैं (switch (day) { case "Monday": ... }), जो अक्सर एक switch उपयोग करने के लिए पहले string को एक numeric code में convert करने से ज़्यादा readable है।
उदाहरण: Switch on String
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Declare day and set it to "Monday"
String day = "Monday";
// Choose a branch based on the value of day
switch (day) {
case "Monday": System.out.println("Start of week"); break;
case "Friday": System.out.println("Almost weekend"); break;
// default runs when no case matches
default: System.out.println("Midweek");
}
}
}
Login to try C/C++/Java/PHP code in the editor
breakभूल जाना, इसलिए execution अगले case में fall through होता है।- एक
Stringयाintcase value उपयोग करना जो constant नहीं, या duplicate case labels, जो compile errors हैं। - Unexpected values के लिए एक
defaultcase भूल जाना।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: