Go If基础教程文档
收录于 2023-04-20 00:10:05 · بالعربية · English · Español · हिंदीName · 日本語 · Русский язык · 中文繁體
Go If
Go 中的 if 语句用于测试条件。如果它的计算结果为真,则执行语句的主体。如果计算结果为 false,则跳过块。
语法:
if(boolean_expression) { /* statement(s) got executed only if the expression results in true */ }
去例子
package main import "fmt" func main() { /* local variable definition */ var a int = 10 /* check the boolean condition using if statement */ if( a % 2==0 ) { /* if condition is true then print the following */ fmt.Printf("a is even number" ) } }
输出:
a is even number
Go if-else
if-else 用于测试条件。如果条件为真,则执行块,否则执行块。
语法:
if(boolean_expression) { /* statement(s) got executed only if the expression results in true */ } else { /* statement(s) got executed only if the expression results in false */ }
转到 if-else 示例
package main import "fmt" func main() { /* local variable definition */ var a int = 10; /* check the boolean condition */ if ( a%2 == 0 ) { /* if condition is true then print the following */ fmt.Printf("a is even\n"); } else { /* if condition is false then print the following */ fmt.Printf("a is odd\n"); } fmt.Printf("value of a is : %d\n", a); }
输出:
a is even value of a is : 10
Go If-else 示例: 来自用户的输入
func main() { fmt.Print("Enter number: ") var input int fmt.Scanln(&input) fmt.Print(input) /* check the boolean condition */ if( input % 2==0 ) { /* if condition is true then print the following */ fmt.Printf(" is even\n" ); } else { /* if condition is false then print the following */ fmt.Printf(" is odd\n" ); } }
输出:
Enter number: 10 10 is even
Go If else-if 链
Go if else-if 链用于从多个条件执行一个语句。
我们可以有N个if-else语句。它没有限制。
大括号{ } 在if-else 语句中是强制性的,即使其中有一个语句。 else-if 和 else 关键字必须在大括号 } 之后的同一行上。
Go If else-if 链示例
package main import "fmt" func main() { fmt.Print("Enter text: ") var input int fmt.Scanln(&input) if (input < 0 || input > 100) { fmt.Print("Please enter valid no") } else if (input >= 0 && input < 50 ) { fmt.Print(" Fail") } else if (input >= 50 && input < 60) { fmt.Print(" D Grade") } else if (input >= 60 && input < 70 ) { fmt.Print(" C Grade") } else if (input >= 70 && input < 80) { fmt.Print(" B Grade") } else if (input >= 80 && input < 90 ) { fmt.Print(" A Grade") } else if (input >= 90 && input <= 100) { fmt.Print(" A+ Grade") } }
输出:
Enter text: 84 A Grade
Go Nested if-else
我们也可以嵌套 if-else 语句以从多个条件执行一个语句。
语法
if( boolean_expression 1) { /* statement(s) got executed only if the expression 1 results in true */ if(boolean_expression 2) { /* statement(s) got executed only if the expression 2 results in true */ } }
嵌套 if-else 示例
package main import "fmt" func main() { /* local variable definition */ var x int = 10 var y int = 20 /* check the boolean condition */ if( x >=10 ) { /* if condition is true then check the following */ if( y >= 10 ) { /* if condition is true then print the following */ fmt.Printf("Inside nested if Statement \n" ); } } fmt.Printf("Value of x is : %d\n", x ); fmt.Printf("Value of y is : %d\n", y ); }
输出:
Inside nested if Statement
Value of x is : 10
Value of y is : 20