Else If Statement

...

Go else if Statement

else if bayonotidan birinchi shart noto'g'ri bo'lsa, yangi shartni aniqlash uchun foydalaniladi.

Sintaksis

if condition1 {
   // condition1 haqiqiy bo'lsa bajariladigan kod
} else if condition2 {
   // condition1 noto'g'ri va condition2 haqiqiy bo'lsa bajariladigan kod
} else {
   // condition1 va condition2 ham noto'g'ri bo'lsa bajariladigan kod
}

else if Bayonotidan Foydalanish

Misol

Bu misol else if bayonotini qanday ishlatishni ko'rsatadi.

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.")
  }
}

Natija

Good evening.

Misolni Tushuntirish

Yuqoridagi misolda, vaqt (22) 10 dan katta, shuning uchun birinchi shart noto'g'ri. Keyingi shart, else if bayonotida, ham noto'g'ri, shuning uchun else shartiga o'tamiz, chunki condition1 va condition2 ham noto'g'ri - va ekranga "Good evening" (Yaxshi kech) deb chiqaramiz.

Agar vaqt 14 bo'lsa, dasturimiz "Good day" (Yaxshi kun) deb chiqarardi.

Misol

else if bayonotining yana bir misoli.

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.")
  }
}

Natija

a and b are equal.

Misol

Eslatma: Agar condition1 va condition2 IKKALA HAQIYQATDA bo'lsa, faqat condition1 uchun kod bajariladi:

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.")
  }
}

Natija

x is larger than or equal to 10.

Ushbu sahifada

Xato haqida xabar berish