Golang教程:(八)if else 语句
u011304970 · · 8885 次点击 · · 开始浏览原文:https://golangbot.com/if-statement/
这是本Golang系列教程的第八篇。
if 是一个条件语句。if 语句的语法为:
if condition {
}
如果 condition 为 true,那么就执行 { 和 } 之间的代码。
与其它语言(如C)不同,即使 {} 之间只有一条语句,{} 也是必需的。
if 语句后面可以接可选的 else if 和 else 语句:
if condition {
} else if condition {
} else {
}
if 后面可以接任意数量的 else if 语句。condition 的求值由上到下依次进行,直到某个 if 或者 else if 中的 condition 为 true 时,执行相应的代码块。如果没有一个 conditon 为 true,则执行 else 中的代码块。
让我们写一个简单的程序来判断一个数是奇数还是偶数:
package main
import (
"fmt"
)
func main() {
num := 10
if num % 2 == 0 { //checks if number is even
fmt.Println("the number is even")
} else {
fmt.Println("the number is odd")
}
}
if num % 2 == 0 这条语句检测一个数除以 2 的余数是否为 0,如果是则输出:"the number is even",否则输出:"the number is odd"。上面的程序输出:the number is even。
if 语句还有如下的变体。这种形式的 if 语句先执行 statement,然后再判断 conditon 。
if statement; condition {
}
让我们用这种形式的 if 改写上面的程序:
package main
import (
"fmt"
)
func main() {
if num := 10; num % 2 == 0 { //checks if number is even
fmt.Println(num,"is even")
} else {
fmt.Println(num,"is odd")
}
}
在上面的程序中, num 在 if 语句中初始化。需要注意的一点是,num 只能在 if 和 else 里面进行访问,即 num 的范围仅限于 if else 块中。如果我们试图在 if 或 else 之外访问 num,编译器将报错。
让我们用 else if 再写一个程序:
package main
import (
"fmt"
)
func main() {
num := 99
if num >= 0 && num <= 50 {
fmt.Println("number is greater than 50")
} else if num >= 51 && num <= 100 {
fmt.Println("number is between 51 and 100")
} else {
fmt.Println("number is greater than 100")
}
}
上面的程序中 else if num >= 51 && num <= 100 为 true,因此程序的输出为:number is between 51 and 100。
if else 语句的介绍到此结束。感谢阅读。
目录
上一篇:Golang教程:(七)包
下一篇:Golang教程:(九)循环语句
有疑问加站长微信联系(非本文作者)
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
关注微信- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码` - 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传
收入到我管理的专栏 新建专栏
原文:https://golangbot.com/if-statement/
这是本Golang系列教程的第八篇。
if 是一个条件语句。if 语句的语法为:
if condition {
}
如果 condition 为 true,那么就执行 { 和 } 之间的代码。
与其它语言(如C)不同,即使 {} 之间只有一条语句,{} 也是必需的。
if 语句后面可以接可选的 else if 和 else 语句:
if condition {
} else if condition {
} else {
}
if 后面可以接任意数量的 else if 语句。condition 的求值由上到下依次进行,直到某个 if 或者 else if 中的 condition 为 true 时,执行相应的代码块。如果没有一个 conditon 为 true,则执行 else 中的代码块。
让我们写一个简单的程序来判断一个数是奇数还是偶数:
package main
import (
"fmt"
)
func main() {
num := 10
if num % 2 == 0 { //checks if number is even
fmt.Println("the number is even")
} else {
fmt.Println("the number is odd")
}
}
if num % 2 == 0 这条语句检测一个数除以 2 的余数是否为 0,如果是则输出:"the number is even",否则输出:"the number is odd"。上面的程序输出:the number is even。
if 语句还有如下的变体。这种形式的 if 语句先执行 statement,然后再判断 conditon 。
if statement; condition {
}
让我们用这种形式的 if 改写上面的程序:
package main
import (
"fmt"
)
func main() {
if num := 10; num % 2 == 0 { //checks if number is even
fmt.Println(num,"is even")
} else {
fmt.Println(num,"is odd")
}
}
在上面的程序中, num 在 if 语句中初始化。需要注意的一点是,num 只能在 if 和 else 里面进行访问,即 num 的范围仅限于 if else 块中。如果我们试图在 if 或 else 之外访问 num,编译器将报错。
让我们用 else if 再写一个程序:
package main
import (
"fmt"
)
func main() {
num := 99
if num >= 0 && num <= 50 {
fmt.Println("number is greater than 50")
} else if num >= 51 && num <= 100 {
fmt.Println("number is between 51 and 100")
} else {
fmt.Println("number is greater than 100")
}
}
上面的程序中 else if num >= 51 && num <= 100 为 true,因此程序的输出为:number is between 51 and 100。
if else 语句的介绍到此结束。感谢阅读。
目录
上一篇:Golang教程:(七)包
下一篇:Golang教程:(九)循环语句