Lessons
Go Introduction
Go Get Started
Go Syntax
Go Comments
Go Variables
- Declare Variables
- Go में Variables का परिचय
- Go में Multiple Variable Declaration
- Variable Naming Rules (नामकरण के नियम)
Go Constants
Go Output
Go Data Types
- Basic Data Types
- Go Boolean Data Type (बूलियन डेटा प्रकार)
- Go Integer Data Types
- Go Float Data Types
- Go String Data Type
Go Arrays
Go Slices
Go Operators
- Go Operators
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Logical Operators
- Bitwise Operators
Go Conditions
Go Switch
Go Loops
Go Functions
Go Struct
Go Maps
Project Structure
Go else if Statement
Go भाषा में else if Statement
else if statement का उपयोग तब किया जाता है जब पहली condition false हो जाती है और आप एक नई condition की जाँच करना चाहते हैं।
सिंटैक्स (Syntax)
if condition1 {
// यह कोड तब चलेगा जब condition1 सही हो
} else if condition2 {
// यह कोड तब चलेगा जब condition1 गलत और condition2 सही हो
} else {
// यह कोड तब चलेगा जब दोनों condition गलत हों
}
else if का उपयोग
यह उदाहरण दिखाता है कि else if का उपयोग कैसे किया जाता है:
package main
import ("fmt")
func main() {
time := 22
if time < 10 {
fmt.Println("Good morning.")
} else if time < 20 {
fmt.Println("Good day.")
} else {
fmt.Println("Good evening.")
}
}
Result:
Good evening.
उदाहरण का स्पष्टीकरण:
ऊपर दिए गए उदाहरण में time (22) 10 से बड़ा है,
इसलिए पहली condition false हो जाती है।
दूसरी condition (time < 20) भी false है,
इसलिए else वाला ब्लॉक चलता है और
स्क्रीन पर "Good evening" प्रदर्शित होता है।
अगर time की वैल्यू 14 होती,
तो प्रोग्राम "Good day." प्रिंट करता।
एक और उदाहरण:
नीचे के उदाहरण में a और b की तुलना की गई है:
package main
import ("fmt")
func main() {
a := 14
b := 14
if a < b {
fmt.Println("a is less than b.")
} else if a > b {
fmt.Println("a is more than b.")
} else {
fmt.Println("a and b are equal.")
}
}
Result:
a and b are equal.
महत्वपूर्ण उदाहरण:
अगर condition1 और condition2 दोनों true हैं, तो केवल पहली condition का कोड ही चलेगा।
package main
import ("fmt")
func main() {
x := 30
if x >= 10 {
fmt.Println("x is larger than or equal to 10.")
} else if x > 20 {
fmt.Println("x is larger than 20.")
} else {
fmt.Println("x is less than 10.")
}
}
Result:
x is larger than or equal to 10.
if की पहली condition true हो जाती है,
तो Go आगे की conditions को नहीं जांचता।
इसे short-circuit evaluation कहा जाता है।