← Back to Go Course | Chapter 3: Control Flow | Lesson 3 of 7

switch Statement क्या है

एक switch किसी value को कई संभव जवाबों के खिलाफ जांचने का एक साफ़ तरीका है, बजाय if/else statements की लंबी chain लिखने के।
Syntax
go
switch expression {
case value1, value2:
    // body
case value3:
    // body
default:
    // body
}

Basic switch

एक switch एक expression को कई case values से compare करता है और matching branch का code चलाता है, फिर अपने आप रुक जाता है -- अगले case में कोई implicit fallthrough नहीं, जो C या Java से आने वाले लोगों के लिए confusion का एक आम point है।

उदाहरण: Basic switch

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the fmt package
import "fmt"

// main is where the program starts running
func main() {
	// Short declaration: create day and infer the type
	day := "Tue"
	// Choose a branch based on the value of day
	switch day {
	case "Mon":
		fmt.Println("Start of week")
	case "Tue", "Wed", "Thu":
		fmt.Println("Midweek")
	// default runs when no case matches
	default:
		fmt.Println("Other day")
	}
}

switch with No Expression

switch के बाद expression छोड़ना हर case को एक boolean condition बना देता है, order में evaluate होते हुए, else-if statements की chain के ज़्यादा साफ़ version की तरह काम करते हुए।

Note: कई conditions होने पर यह form एक लंबी if/else-if chain से ज़्यादा साफ़ पढ़ा जाता है।

उदाहरण: switch with No Expression

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the fmt package
import "fmt"

// main is where the program starts running
func main() {
	// Short declaration: create score and infer the type
	score := 82
	// Choose a branch based on the value
	switch {
	case score >= 90:
		fmt.Println("A")
	case score >= 75:
		fmt.Println("B")
	// default runs when no case matches
	default:
		fmt.Println("C")
	}
}

Using fallthrough

अगर आप चाहते हैं कि कोई case अगले में जारी रहे, Go आपको fallthrough keyword से explicitly ऐसा कहने के लिए कहता है, उस (असामान्य) behavior को code में दिखाई देने योग्य बनाते हुए बजाय एक आसानी से miss हो जाने वाले default के।

Note: fallthrough की शायद ही कभी ज़रूरत पड़ती है -- इसका सहारा सिर्फ तभी लें जब आप जान-बूझकर चाहते हों कि अगले case का code भी चले।

उदाहरण: Using fallthrough

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the fmt package
import "fmt"

// main is where the program starts running
func main() {
	// Short declaration: create n and infer the type
	n := 1
	// Choose a branch based on the value of n
	switch n {
	case 1:
		fmt.Println("one")
		fallthrough
	case 2:
		fmt.Println("two (via fallthrough)")
	case 3:
		fmt.Println("three")
	}
}

switch with an Init Statement

if की तरह, एक switch उस value से पहले एक छोटा statement चला सकता है जिस पर यह switch करता है, एक variable को पूरे switch block तक scope करते हुए -- एक value को एक बार compute करने और उस पर branch करने के लिए सुविधाजनक।

उदाहरण: switch with an Init Statement

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the fmt package
import "fmt"

// Define the function classify
func classify(n int) string {
	// Choose a branch based on the value of remainder := n % 3; remainder
	switch remainder := n % 3; remainder {
	case 0:
		return "divisible by 3"
	// default runs when no case matches
	default:
		return "not divisible by 3"
	}
}

// main is where the program starts running
func main() {
	// Print the values followed by a newline
	fmt.Println(classify(9))
	// Print the values followed by a newline
	fmt.Println(classify(10))
}
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. C/Java की आदत से हर case के आखिर में एक explicit break जोड़ना -- Go के cases default रूप से fall through नहीं करते, इसलिए यह अनावश्यक है।
  2. यह भूल जाना कि Go का switch बिल्कुल बिना किसी expression के भी उपयोग किया जा सकता है, एक लंबी if/else if chain के साफ़ विकल्प के रूप में।
  3. fallthrough के बारे में न जानना और इसे छोड़ने पर हैरान होना कि अगला case अपने आप क्यों नहीं चलता।
चैप्टर सारांश
  • Go का switch हर case के बाद अपने आप break हो जाता है -- जब तक आप fallthrough keyword से न मांगें कोई fallthrough नहीं।
  • बिना किसी expression वाला switch if/else if statements की एक साफ़ chain जैसा काम करता है।
  • एक single case कई comma-separated values list कर सकता है।
  • switch एक init statement शामिल कर सकता है, बिल्कुल if की तरह।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

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.