最适合网络开发者的网站
Golang。初学者课程

尿素

Go Nested if Statement


The Nested if Statement

You can have if statements inside if statements, this is called a nested if.

Syntax

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

例子

This example shows how to use nested if statements:

软件包 main
导入(“fmt”)

函数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.
亲自尝试 »