← Back to Core Java Course | Chapter 4: Control Flow | Lesson 4 of 9

Java का switch Statement

एक switch statement एक value के आधार पर एक menu से एक action चुनता है, एक vending machine पर button press करने जैसा। हर case एक button है, और break इसे अगली में चलने से रोकता है।
Syntax
java
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

java
// 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;
		}
	}
}

The Importance of break

एक matching case के आखिर में एक break के बिना, execution अगले case के code में 'fall through' हो जाता है भले ही इसकी अपनी condition न जांची गई हो -- यह switch statements में नए developers के लिए bugs का एक आम source है।

उदाहरण: The Importance of break

java
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;
		}
	}
}

Using the default Case

default else का switch equivalent है, सिर्फ तब चलता है जब कोई explicit case value switch के expression से match न हो -- unexpected values को gracefully handle करने के लिए हमेशा एक शामिल करना अच्छा practice है।

उदाहरण: Using the default Case

java
// 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;
		}
	}
}

Grouping Switch Cases

कई case labels को बीच में कोई code न रखते हुए एक-दूसरे के पीछे stack करना, जैसे case 1: case 2: doSomething(); break;, 1 और 2 दोनों को वही shared block trigger कराता है बजाय उस code को दो बार duplicate करने के।

उदाहरण: Grouping Switch Cases

java
// 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");
		}
	}
}

Switch on String

Java 7 से, आप सीधे एक String value पर switch कर सकते हैं (switch (day) { case "Monday": ... }), जो अक्सर एक switch उपयोग करने के लिए पहले string को एक numeric code में convert करने से ज़्यादा readable है।

उदाहरण: Switch on String

java
// 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");
		}
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. break भूल जाना, इसलिए execution अगले case में fall through होता है।
  2. एक String या int case value उपयोग करना जो constant नहीं, या duplicate case labels, जो compile errors हैं।
  3. Unexpected values के लिए एक default case भूल जाना।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.