Mr. Deepak Verma
Web Developer
Go else if Statement

Learn Go Language by M-Learnify

Go Language

Go else if Statement


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 कहा जाता है।
← Back to Courses
Course Lessons