Go语言中的if条件语句使用详解
if语句 语法 复制代码 代码如下: if(boolean_expression) { /* statement(s) will execute if the boolean expression is true */ } 如果布尔表达式的值为 true,那么if语句里面代码块将被执行。如果if语句的结束(右大括号后)布尔表达式的值为false,那么语句之后第一行代码会被执行。 流程图: 例子: 复制代码 代码如下: package main import "fmt" func main() { 让我们编译和运行上面的程序,这将产生以下结果: a is less than 20; value of a is : 10
语法 复制代码 代码如下: if(boolean_expression) { /* statement(s) will execute if the boolean expression is true */ } else { /* statement(s) will execute if the boolean expression is false */ } 如果布尔表达式的值为true,那么if代码块将被执行,否则else代码块将被执行。 流程图: 例子: 复制代码 代码如下: package main import "fmt" func main() { } 当上述代码被编译和执行时,它产生了以下结果: a is not less than 20; value of a is : 100 if...else if...else 语句 当使用if,else if,else语句有几点要记住使用: if可以有零或一个else,它必须跟从else if后面。 一个if可以有零到个多else if并且它们必须在else之前。 一旦一个else if测试成功,其它任何剩余else if将不会被测试。 语法 复制代码 代码如下: if(boolean_expression 1) { /* Executes when the boolean expression 1 is true */ } else if( boolean_expression 2) { /* Executes when the boolean expression 2 is true */ } else if( boolean_expression 3) { /* Executes when the boolean expression 3 is true */ } else { /* executes when the none of the above condition is true */ } 例子: 复制代码 代码如下: package main import "fmt" func main() { 让我们编译和运行上面的程序,这将产生以下结果: None of the values is matching Exact value of a is: 100 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |