Latest web development tutorials

Go language if statement nested

Go language conditional statements Go language conditional statements

You can embed one or more of if or else if statement if or else if statement.

grammar

Go programming language if ... else statement syntax is as follows:

if 布尔表达式 1 {
   /* 在布尔表达式 1 为 true 时执行 */
   if 布尔表达式 2 {
      /* 在布尔表达式 2 为 true 时执行 */
   }
}

In the same way you can be nestedelse if ... else statement if statement

Examples

package main

import "fmt"

func main() {
   /* 定义局部变量 */
   var a int = 100
   var b int = 200
 
   /* 判断条件 */
   if a == 100 {
       /* if 条件语句为 true 执行 */
       if b == 200 {
          /* if 条件语句为 true 执行 */
          fmt.Printf("a 的值为 100 , b 的值为 200\n" );
       }
   }
   fmt.Printf("a 值为 : %d\n", a );
   fmt.Printf("b 值为 : %d\n", b );
}

The above code is executed as a result of:

a 的值为 100 , b 的值为 200
a 值为 : 100
b 值为 : 200

Go language conditional statements Go language conditional statements