← Back to C++ Course | Chapter 4: Control Flow | Lesson 8 of 13

C++ का switch Statement

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

Basic switch Statement

एक switch statement एक variable की value को कई possible constant cases से compare करता है और सीधे matching case पर jump करता है, जो एक single value को कई discrete options जैसे एक menu choice या day number से check करते समय एक लंबे else-if ladder से ज़्यादा cleanly पढ़ता है।

उदाहरण: Basic switch Statement

cpp
// Include std::cout and std::cin
#include <iostream>

// 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 1:
			std::cout << "Monday" << std::endl;
			// Leave the switch/loop
			break;
		case 2:
			std::cout << "Tuesday" << std::endl;
			// Leave the switch/loop
			break;
		case 3:
			std::cout << "Wednesday" << std::endl;
			break;
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

break Statement

break एक matching case का code चलने के बाद तुरंत switch block से exit हो जाता है; इसे छोड़ना execution को अगले case के code में भी 'fall through' कराता है, जो कभी-कभी intentional होता है लेकिन भूल जाने पर bugs का एक बहुत common source है।

जब तक fall-through आपके design का जानबूझकर हिस्सा न हो हमेशा break add करें।

उदाहरण: The break Statement

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare day and set it to 1
	int day = 1;
	// Choose a branch based on the value of day
	switch (day) {
		case 1:
			std::cout << "Monday" << std::endl;
		case 2:
			std::cout << "Falls through to Tuesday" << std::endl;
			// Leave the switch/loop
			break;
		case 3:
			std::cout << "Wednesday" << std::endl;
			// Leave the switch/loop
			break;
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

default Case इस्तेमाल करना

default case किसी भी value को पकड़ता है जो explicit case labels में से किसी से match नहीं करती, एक if-else ladder में एक final else के switch statement equivalent की तरह काम करते हुए।

एक default case शामिल करना तब भी अच्छी practice है जब आप unmatched values की उम्मीद न करें, क्योंकि यह unexpected input को चुपचाप ignore होने की बजाय visible बनाता है।

उदाहरण: Using the default Case

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare day and set it to 9
	int day = 9;
	// Choose a branch based on the value of day
	switch (day) {
		case 1:
			std::cout << "Monday" << std::endl;
			// Leave the switch/loop
			break;
		// default runs when no case matches
		default:
			std::cout << "Invalid day" << std::endl;
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

switch Cases Group करना

एक shared block के ठीक ऊपर कई case labels stack करना, जैसे case 1: case 2: case 3: के बाद code का एक block, कई distinct values को हर एक के लिए code duplicate किए बिना identical behavior trigger करने देता है।

यह same fall-through behavior पर depend करता है जिसे break normally रोकता है, यहाँ जानबूझकर इस्तेमाल किया गया।

उदाहरण: Grouping switch Cases

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare day and set it to 6
	int day = 6;
	// Choose a branch based on the value of day
	switch (day) {
		case 1:
		case 2:
		case 3:
		case 4:
		case 5:
			std::cout << "Weekday" << std::endl;
			// Leave the switch/loop
			break;
		case 6:
		case 7:
			std::cout << "Weekend" << std::endl;
			// Leave the switch/loop
			break;
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Nested switch Statements

एक switch statement को दूसरे switch के case के अंदर रखना layered categorical decisions handle करने के लिए valid है, जैसे पहले एक product category पर और फिर इसके अंदर एक specific product पर branch करना।

Nested switches जल्दी follow करना मुश्किल हो सकते हैं, इसलिए यह consider करना worth है कि अलग functions logic को ज़्यादा clear रखेंगे या नहीं।

उदाहरण: Nested switch Statements

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare category and set it to 1
	int category = 1;
	// Declare size and set it to 2
	int size = 2;
	// Choose a branch based on the value of category
	switch (category) {
		case 1:
			switch (size) {
				case 1:
					std::cout << "Small shirt" << std::endl;
					// Leave the switch/loop
					break;
				case 2:
					std::cout << "Medium shirt" << std::endl;
					// Leave the switch/loop
					break;
			}
			break;
	}
	// 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 में एक non-constant या string इस्तेमाल करना, जब case labels को integral constants होना चाहिए।
  3. braces के बिना एक case के अंदर एक variable declare करना, जो एक 'jump to case label' error cause कर सकता है।

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.