← Back to C Course | Chapter 4: Control Flow | Lesson 6 of 11

C का switch Statement

एक switch statement एक value के आधार पर एक menu से एक action चुनता है, एक vending machine पर button दबाने जैसा। break इसे अगले choice में चलने से रोकता है।
Syntax
c
switch (variable) {
  case value1:
    // code
    break;
  case value2:
    // code
    break;
  default:
    // code if no case matches
}

switch Statement क्या है?

एक switch statement एक expression को एक बार evaluate करता है और इसे case labels की एक list से compare करता है, सीधे matching case पर jump करते हुए -- जब आप एक variable को कई fixed values से compare कर रहे हों तो एक लंबे else-if ladder का एक cleaner alternative।

उदाहरण: What is a switch Statement?

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare day and set it to 3
	int day = 3;
	// Choose a branch based on the value of day
	switch (day) {
		case 3:
			printf("Wednesday");
			// Leave the switch/loop
			break;
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Day of the Week

1 से 7 तक integers को day names पर map करना एक classic switch use case है: हर case label एक specific number handle करता है, mapping को equivalent if-else comparisons की chain से कहीं ज़्यादा readable बनाते हुए।

उदाहरण: Day of the Week

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare day and set it to 2
	int day = 2;
	// Choose a branch based on the value of day
	switch (day) {
		case 1: printf("Monday"); break;
		case 2: printf("Tuesday"); break;
		case 3: printf("Wednesday"); break;
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Grade Switch

switch character constants भी match कर सकता है, इसलिए एक grade switch case A को "Excellent" print करने के लिए map कर सकता है, आपको integers जितनी ही आसानी से single-character input पर branch करने देते हुए।

उदाहरण: Grade Switch

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare grade and set it to 'A'
	char grade = 'A';
	// Choose a branch based on the value of grade
	switch (grade) {
		case 'A':
			printf("Excellent");
			// Leave the switch/loop
			break;
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

break Keyword

एक case block के अंत में explicit break के बिना, execution "fall through" होता है और अगले case में भी code चलाना जारी रखता है -- ऐसी language से आने वाले किसी के लिए एक frequent source of bugs जहाँ fall-through default नहीं है।

उदाहरण: The break Keyword

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare x and set it to 1
	int x = 1;
	// Choose a branch based on the value of x
	switch (x) {
		case 1:
			printf("One ");
		case 2:
			printf("Falls through to Two");
			// Leave the switch/loop
			break;
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

default Case

default case तब चलता है जब switch expression listed case values में से किसी से match नहीं करता, एक catch-all की तरह काम करते हुए -- unexpected input handle करने या invalid values explicitly flag करने के लिए useful।

उदाहरण: The default Case

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare x and set it to 9
	int x = 9;
	// Choose a branch based on the value of x
	switch (x) {
		case 1:
			printf("One");
			// Leave the switch/loop
			break;
		// default runs when no case matches
		default:
			printf("Unknown value");
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}
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. एक case label की तरह एक string या variable इस्तेमाल करना, जब labels को integer constants होना चाहिए।
  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.