Go 语言 if 语句嵌套

更新时间: 2019-08-16 09:12

你可以在 if 或 else if 语句中嵌入一个或多个 if 或 else if 语句。

语法

Go 编程语言中 if...else 语句的语法如下:

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

你可以以同样的方式在 if 语句中嵌套 else if...else 语句

实例

嵌套使用 if 语句:

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 );
}

以上代码执行结果为:

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

查看更多 Go 语言条件语句

Automate This

Automate This

Christopher Steiner / Portfolio / 2013-8-9 / USD 25.95

"The rousing story of the last gasp of human agency and how today's best and brightest minds are endeavoring to put an end to it." It used to be that to diagnose an illness, interpret legal docume......一起来看看 《Automate This》 这本书的介绍吧!

JS 压缩/解压工具

JS 压缩/解压工具

在线压缩/解压 JS 代码

html转js在线工具

html转js在线工具

html转js在线工具

HEX HSV 转换工具

HEX HSV 转换工具

HEX HSV 互换工具