嵌套 if 语句


嵌套的 if 语句

你可以有if里面的陈述if语句,这称为嵌套 if。

语法

if condition1 {
   // code to be executed if condition1 is true
  if condition2 {
     // code to be executed if both condition1 and condition2 are true
  }
}

示例

这个例子展示了如何使用嵌套if声明:

package main
import ("fmt")

func main() {
  num := 20
  if num >= 10 {
    fmt.Println("Num is more than 10.")
    if num > 15 {
      fmt.Println("Num is also more than 15.")
     }
  } else {
    fmt.Println("Num is less than 10.")
  }
}

结果:

Num is more than 10.
Num is also more than 15.
亲自试一试 »