Golang教程:(八)if else 语句
原文:https://golangbot.com/if-statement/ 这是本Golang系列教程的第八篇。 if 是一个条件语句。if 语句的语法为: if condition {
}
如果 与其它语言(如C)不同,即使
if condition {
} else if condition {
} 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 statement; condition {
}
让我们用这种形式的 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")
}
}
在上面的程序中, 让我们用 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")
}
}
上面的程序中 if else 语句的介绍到此结束。感谢阅读。 目录 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |