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 if else Statement
Go भाषा में else Statement
else statement का उपयोग तब किया जाता है जब किसी if condition का परिणाम false आता है।
ऐसी स्थिति में else के अंदर लिखा गया कोड ब्लॉक चलाया जाता है।
सिंटैक्स (Syntax)
if condition {
// यह कोड तब चलेगा जब condition true होगी
} else {
// यह कोड तब चलेगा जब condition false होगी
}
if else का उपयोग
नीचे दिए उदाहरण में, time की वैल्यू 20 है जो 18 से बड़ी है,
इसलिए if की condition false हो जाएगी और else का कोड चलेगा।
package main
import ("fmt")
func main() {
time := 20
if (time < 18) {
fmt.Println("Good day.")
} else {
fmt.Println("Good evening.")
}
}
अगर time की वैल्यू 18 से कम होती, तो प्रोग्राम "Good day" प्रिंट करता।
एक और उदाहरण:
इस उदाहरण में temperature की वैल्यू 14 है,
इसलिए if की शर्त false हो जाती है और else ब्लॉक चलता है।
package main
import ("fmt")
func main() {
temperature := 14
if (temperature > 15) {
fmt.Println("It is warm out there")
} else {
fmt.Println("It is cold out there")
}
}
महत्वपूर्ण:
else स्टेटमेंट में ब्रैकेट्स का क्रम } else { होना चाहिए।
अगर आप else को नई लाइन पर लिखते हैं, तो syntax error आएगा।
else नई लाइन में लिखा गया है,
जिससे Go compiler error देगा।
package main
import ("fmt")
func main() {
temperature := 14
if (temperature > 15) {
fmt.Println("It is warm out there.")
} // यह error देगा
else {
fmt.Println("It is cold out there.")
}
}
Result:
./prog.go:9:3: syntax error: unexpected else, expecting }