switch Statement क्या है
In this page:
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
// 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")
}
}
Login to try C/C++/Java/PHP code in the editor
switch with No Expression
switch के बाद expression छोड़ना हर case को एक boolean condition बना देता है, order में evaluate होते हुए, else-if statements की chain के ज़्यादा साफ़ version की तरह काम करते हुए।
उदाहरण: switch with No Expression
// 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")
}
}
Login to try C/C++/Java/PHP code in the editor
Using fallthrough
अगर आप चाहते हैं कि कोई case अगले में जारी रहे, Go आपको fallthrough keyword से explicitly ऐसा कहने के लिए कहता है, उस (असामान्य) behavior को code में दिखाई देने योग्य बनाते हुए बजाय एक आसानी से miss हो जाने वाले default के।
उदाहरण: Using fallthrough
// 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")
}
}
Login to try C/C++/Java/PHP code in the editor
switch with an Init Statement
if की तरह, एक switch उस value से पहले एक छोटा statement चला सकता है जिस पर यह switch करता है, एक variable को पूरे switch block तक scope करते हुए -- एक value को एक बार compute करने और उस पर branch करने के लिए सुविधाजनक।
उदाहरण: switch with an Init Statement
// 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))
}
Login to try C/C++/Java/PHP code in the editor
- C/Java की आदत से हर case के आखिर में एक explicit break जोड़ना -- Go के cases default रूप से fall through नहीं करते, इसलिए यह अनावश्यक है।
- यह भूल जाना कि Go का switch बिल्कुल बिना किसी expression के भी उपयोग किया जा सकता है, एक लंबी if/else if chain के साफ़ विकल्प के रूप में।
- 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: