Go语言 if语句


导言

  • 原文链接: Part 8: if else statement
  • If translation is not allowed, please leave me in the comment area and I will delete it as soon as possible.

if语句

介绍

if 是条件语句,语法如下:

if condition {  
	// 代码块
}

如果 condition 等于 trueif 的代码块会被执行。

C 语言不同,在 Go语言 中,即使代码块之间只有一条语句,{} 也不能被省略。

if语句 还能加入 elseelse if 组件。

if condition {  
} else if condition {
} else {
}

if语句 的 conditionfalse 时,程序就会执行 离它最近的 else语句 的代码。

我们可以添加无数个 else if 组件,程序会从上到下判断这些组件的 condition ,直到遇到 等于truecondition,此时程序执行其对应的代码块。

我们来写个程序,判断数的奇偶性:

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语句,我们可以判断 num / 2 的余数是否为 0,如果为 0 ,则执行 fmt.Println("the number is even"),否则执行 fmt.Println("the number is odd")

运行程序,输出如下:

the number is even

这里还有一个 if语句 的变体,这个变体加入了 statement组件 — statement语句 会在 condition 判断前执行。句式如下:

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")
    }
}

在上面的程序中,numif语句 中初始化。这里需要注意:此时,我们只能在 if else代码块 中访问 变量num。即,num 的作用域被限制在 if else代码块 之中。如果在 if else代码块 之外访问 num,编译器会报错。

接下来,让我们使用 else if 写个程序:

package main

import (  
    "fmt"
)

func main() {  
    num := 99
    if num <= 50 {
        fmt.Println("number is less than or equal to 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 <= 100true,所以程序将会输出:

number is between 51 and 100

疑难杂症

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")
    }
}

在上面的代码中, else 并没有和 } 在同一行,而是在 } 的下一行。Go语言 不允许这样的情况发生。

运行程序,编译器会输出如下错误:

main.go:12:5: syntax error: unexpected else, expecting }  

为什么 else} 要在同一行呢?这与 Go分号自动插入机制 有关。根据规则,当 } 处于行末时,编译器会在 } 之后插入分号。

于是,我们的代码会变为:

if num%2 == 0 {  
      fmt.Println("the number is even") 
};  //semicolon inserted by Go
else {  
      fmt.Println("the number is odd")
};

因为 if{...}else {...} 构成了一个整体,它们之间不应有分号,所以我们只能把 else 放在最近的 } 之后,从而防止编译器在 if{...}else {...} 之间插入分号。

下面,我把 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")
    }
}

现在程序就可以正常运行了,输出如下:

the number is even

这就是 if语句 了~

祝你越来越可爱~


原作者留言

优质内容来之不易,您可以通过该 链接 为我捐赠。


最后

感谢原作者的优质内容。

欢迎指出文中的任何错误。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值