Go - if 语句
if语句由一个布尔表达式后跟一个或多个语句组成。
句法
Go 编程语言中if语句的语法是 -
if(boolean_expression) {
/* statement(s) will execute if the boolean expression is true */
}
如果布尔表达式的计算结果为true,则执行 if 语句内的代码块。如果布尔表达式的计算结果为false,则执行 if 语句末尾(右大括号之后)后的第一组代码。
流程图
例子
package main
import "fmt"
func main() {
/* local variable definition */
var a int = 10
/* check the boolean condition using if statement */
if( a < 20 ) {
/* if condition is true then print the following */
fmt.Printf("a is less than 20\n" )
}
fmt.Printf("value of a is : %d\n", a)
}
当上面的代码被编译并执行时,它会产生以下结果 -
a is less than 20; value of a is : 10
go_decision_making.htm