What are Conditional Statements in Go?
Conditional statements का उपयोग program के flow को control करने के लिए किया जाता है। ये statements allow करते हैं कि program अलग-अलग conditions के आधार पर अलग-अलग actions perform कर सके। Go में commonly used conditional statements हैं:
- if statement
- if-else statement
- if-else if-else ladder
- switch statement
1. if Statement
if statement का उपयोग किसी condition को check करने के लिए किया जाता है। अगर condition true है, तो associated block of code execute होता है।
Example: Basic if Statement
package main
import "fmt"
func main() {
age := 20
if age >= 18 {
fmt.Println("You are an adult.")
}
}
Explanation:
if age >= 18:यहां condition check की जाती है कि age 18 से ज्यादा या बराबर है या नहीं।- अगर condition true है, तो "You are an adult." message print होगा।
2. if-else Statement
if-else statement का उपयोग तब किया जाता है जब आप चाहते हैं कि एक condition के true होने पर एक block of code execute हो, और condition के false होने पर दूसरा block of code execute हो।
Example: if-else Statement
package main
import "fmt"
func main() {
age := 16
if age >= 18 {
fmt.Println("You are an adult.")
} else {
fmt.Println("You are a minor.")
}
}
Explanation:
- अगर
age >= 18condition true होती, तो पहला block execute होता। - लेकिन अगर condition false है, तो
elseblock execute होता है और "You are a minor." message print होता है।
3. if-else if-else Ladder
जब आपको multiple conditions को check करना हो, तब आप if-else if-else ladder का उपयोग कर सकते हैं। यह code को organize और readable रखने में मदद करता है।
Example: if-else if-else Ladder
package main
import "fmt"
func main() {
score := 85
if score >= 90 {
fmt.Println("Grade: A")
} else if score >= 80 {
fmt.Println("Grade: B")
} else if score >= 70 {
fmt.Println("Grade: C")
} else {
fmt.Println("Grade: F")
}
}
Explanation:
- यहां multiple conditions को check किया गया है।
- पहला
ifblock check करता है कि क्या score 90 या उससे ज्यादा है। - अगर ये condition false होती है, तो next
else ifblock check होता है, और इसी तरह आगे बढ़ता है। - अगर कोई भी condition true नहीं होती, तो
elseblock execute होता है।
4. switch Statement
switch statement का उपयोग multiple conditions को handle करने के लिए किया जाता है। यह if-else if-else ladder का अधिक readable और organized विकल्प है।
Example: Basic switch Statement
package main
import "fmt"
func main() {
day := "Monday"
switch day {
case "Monday":
fmt.Println("Start of the work week.")
case "Wednesday":
fmt.Println("Midweek day.")
case "Friday":
fmt.Println("End of the work week.")
default:
fmt.Println("Another day.")
}
}
Explanation:
switch day:यह variabledayकी value के आधार पर cases को match करता है।- अगर
dayकी value "Monday" है, तो "Start of the work week." print होगा। - अगर कोई case match नहीं करता है, तो
defaultblock execute होता है, जो "Another day." print करेगा।
5. switch with Multiple Expressions
Go में आप एक ही case में multiple expressions का use कर सकते हैं। इससे आप conditions को compact और readable बना सकते हैं।
Example: switch with Multiple Expressions
package main
import "fmt"
func main() {
day := "Saturday"
switch day {
case "Saturday", "Sunday":
fmt.Println("It's the weekend!")
default:
fmt.Println("It's a weekday.")
}
}
Explanation:
- यहां "Saturday" और "Sunday" दोनों के लिए एक ही case block execute होता है।
- अगर
dayकी value "Saturday" या "Sunday" है, तो "It's the weekend!" print होगा। बाकी सभी cases में "It's a weekday." print होगा।
6. switch with Conditions
Go में switch statement को conditions के साथ भी use किया जा सकता है, जिससे यह और अधिक flexible हो जाता है।
Example: switch with Conditions
package main
import "fmt"
func main() {
number := 15
switch {
case number < 0:
fmt.Println("Negative number")
case number == 0:
fmt.Println("Zero")
case number > 0:
fmt.Println("Positive number")
}
}
Explanation:
- यहां
switchstatement के अंदर कोई expression नहीं है, लेकिन प्रत्येक case में एक condition है। - इस example में, appropriate condition के आधार पर "Negative number", "Zero", या "Positive number" print होगा।
Summary
- if Statement: Simple conditions को check करने के लिए।
- if-else Statement: एक condition true होने पर एक block execute करने और false होने पर दूसरा block execute करने के लिए।
- if-else if-else Ladder: Multiple conditions को handle करने के लिए।
- switch Statement: Multiple conditions के साथ काम करने के लिए, और इसे
if-elseladder के अधिक readable विकल्प के रूप में उपयोग किया जाता है। - switch with Multiple Expressions: एक case के लिए multiple values को handle करने के लिए।
- switch with Conditions:
switchstatement के साथ conditions का उपयोग करने के लिए।
Go में conditional statements आपके code की flow को control करने का एक शक्तिशाली तरीका हैं। इनका उपयोग करके आप आसानी से complex logic implement कर सकते हैं। अगर आपको इन statements के बारे में और जानकारी चाहिए या कोई specific सवाल है, तो बताइए!
No comments:
Post a Comment