Mr. Deepak Verma
Web Developer
Go if else Statement

Learn Go Language by M-Learnify

Go Language

Go if else Statement


Go भाषा में 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 }
← Back to Courses
Course Lessons