- Go 教程
- Go - 首页
- Go - 概述
- Go - 环境设置
- Go - 程序结构
- Go - 基本语法
- Go - 数据类型
- Go - 变量
- Go - 常量
- Go - 运算符
- Go - 决策
- Go - 循环
- Go - 函数
- Go - 作用域规则
- Go - 字符串
- Go - 数组
- Go - 指针
- Go - 结构
- Go - 切片
- Go - 范围
- Go - 映射
- Go - 递归
- Go - 类型转换
- Go - 接口
- Go - 错误处理
- Go 实用资源
- Go - 常见问题解答
- Go - 快速指南
- Go - 实用资源
- Go - 讨论
Go - 嵌套 if 语句
在 Go 编程中,嵌套 if-else 语句始终是合法的,这意味着你可以在一个 if 或 else if 语句中使用另一个 if 或 else if 语句。
语法
嵌套 if 语句的语法如下 −
if( boolean_expression 1) {
/* Executes when the boolean expression 1 is true */
if(boolean_expression 2) {
/* Executes when the boolean expression 2 is true */
}
}
你可以按照嵌套 if 语句的方式,类似地嵌套 else if...else。
示例
package main
import "fmt"
func main() {
/* local variable definition */
var a int = 100
var b int = 200
/* check the boolean condition */
if( a == 100 ) {
/* if condition is true then check the following */
if( b == 200 ) {
/* if condition is true then print the following */
fmt.Printf("Value of a is 100 and b is 200\n" );
}
}
fmt.Printf("Exact value of a is : %d\n", a );
fmt.Printf("Exact value of b is : %d\n", b );
}
当以上代码编译并执行时,将产生以下结果 −
Value of a is 100 and b is 200 Exact value of a is : 100 Exact value of b is : 200
go_decision_making.htm
广告