if / else के Statements
if condition {
// body
} else if condition2 {
// body
} else {
// body
}
if init; condition {
// body
}
Basic if / else
एक if statement सिर्फ तभी एक block चलाता है जब इसकी condition true evaluate हो, और एक optional else block अन्यथा चलता है। C या Java के विपरीत, condition को कभी parentheses में wrap नहीं किया जाता, और braces हमेशा ज़रूरी हैं, एक single statement के लिए भी।
उदाहरण: Basic if / else
// 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 age and infer the type
age := 20
// Check whether age >= 18
if age >= 18 {
// Print the values followed by a newline
fmt.Println("adult")
} else {
// Print the values followed by a newline
fmt.Println("minor")
}
}
Login to try C/C++/Java/PHP code in the editor
else if Chains
कई conditions को 'else if' के साथ chain किया जा सकता है, ऊपर से नीचे evaluate होते हुए जब तक कोई एक true न हो, कोई मेल न खाने पर एक आखिरी else पर गिरते हुए -- कई mutually exclusive cases handle करने के लिए उपयोगी।
उदाहरण: else if Chains
// 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 := 72
// Check whether score >= 90
if score >= 90 {
// Print the values followed by a newline
fmt.Println("Grade A")
} else if score >= 75 {
// Print the values followed by a newline
fmt.Println("Grade B")
} else if score >= 60 {
fmt.Println("Grade C")
} else {
fmt.Println("Grade F")
}
}
Login to try C/C++/Java/PHP code in the editor
if with an Init Statement
Go आपको condition से पहले, एक semicolon से अलग, एक छोटा statement चलाने देता है, जो सबसे अक्सर एक value assign करने और तुरंत इसे test करने के लिए उपयोग होता है -- एक value और एक error return करने वाले functions के साथ बहुत आम pattern।
उदाहरण: if 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 half
func half(n int) (int, bool) {
// Check whether n%2 != 0
if n%2 != 0 {
// Send 0, false back to the caller
return 0, false
}
// Send n / 2, true back to the caller
return n / 2, true
}
// main is where the program starts running
func main() {
// Check whether result, ok := half(10); ok
if result, ok := half(10); ok {
fmt.Println("half is:", result)
} else {
fmt.Println("not evenly divisible")
}
}
Login to try C/C++/Java/PHP code in the editor
Boolean Expressions
Conditions comparisons को && (and), || (or), और ! (not) के साथ combine करते हैं, बिल्कुल ज़्यादातर C-family languages की तरह, और Go इन operators को short-circuit करता है, result तय होते ही evaluation रोकते हुए।
उदाहरण: Boolean Expressions
// 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 age and infer the type
age := 25
// Short declaration: create hasID and infer the type
hasID := true
// Check whether age >= 18 && hasID
if age >= 18 && hasID {
// Print the values followed by a newline
fmt.Println("entry allowed")
}
}
Login to try C/C++/Java/PHP code in the editor
- condition को 'if (x > 5)' जैसे parentheses में wrap करना -- Go इसकी अनुमति देता है लेकिन idiomatic style हमेशा इन्हें छोड़ देती है।
- यह भूल जाना कि opening brace if/else के साथ उसी line पर होनी चाहिए -- Go का automatic semicolon insertion अन्यथा टूट जाता है।
- यह न समझना कि if के init statement में declared variable (if x := f(); x > 0) सिर्फ if/else chain तक scoped है, बाहर नहीं।
- if/else एक boolean condition के आधार पर branch करता है, condition के चारों ओर parentheses की ज़रूरत नहीं।
- एक optional init statement condition से पहले चल सकता है, एक variable को पूरी if/else chain तक scope करते हुए।
- else if chains कई conditions को साथ जोड़ती हैं।
- Go में braces अनिवार्य हैं, कुछ C-family languages के विपरीत जो बिना उनके single-statement bodies की अनुमति देती हैं।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: