Warning: Undefined array key "custom_message" in /www/wwwroot/bbs.aaronyang.cc/wp-content/plugins/wpcopyrights/index.php on line 105

if else 语句

if else 语句

上一节:第七篇 包
下一节:第九篇 循环语句

这是本Golang系列教程的第8篇。

if 是一个条件语句。if 语句的语法为:

if condition {  
}

如果 condition 为 true,那么就执行 { 和 } 之间的代码。与其它语言(如C)不同,即使 {} 之间只有一条语句,{} 也是必需的。

if 语句后面可以接可选的 else if 和 else 语句:

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

if 后面可以接任意数量的 else if 语句。condition 的求值由上到下依次进行,直到某个 if 或者 else if 中的 conditiontrue 时,执行相应的代码块。如果没有一个 conditontrue,则执行 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")
    }
}

在上面的程序中, numif 语句中初始化。需要注意的一点是,num 只能在 if 和 else 里面进行访问,即 num 的范围仅限于 if else 块中。如果我们试图在 ifelse 之外访问 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 语句应该在 if 语句的大括号 } 之后的同一行中开始,否则编译器会报错。

让我们通过一个程序来理解这一点。

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 语句没有在 if 语句 } 结束后的同一行中开始,而是从下一行开始的。Go中不允许这样做。如果你运行这个程序,编译器会输出错误:

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

这是由于Go会自动插入分号,点击链接了解分号插入规则:https://golang.org/ref/spec#Semicolons

在分号插入规则中,如果 } 是当前一行的最后一个字符,那么 } 后面会被插入一个分号。所以在上面的例子中,if语句的 } 之后会被自动插入一个分号。

所以实际上我们的程序变成了:

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

你可以看到第三行中插入了一个分号。

由于 if{...} else {...} 是一个单独的语句,分号不应该出现在语句的中间。因此,else 需要放在 } 之后的同一行中。

我重新改写了程序,将 else 移动到if语句结束之后的 } 的后面,以防止自动分号插入。

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

现在编译器就可以正常编译我们的程序了。

希望你喜欢阅读。请留下宝贵的意见和反馈:)

若文章对你有帮助,可以点赞或打赏支持我们。发布者:Aurora,转载请注明出处:http://61.174.243.28:13541/AY-knowledg-hub/8-if-else-%e8%af%ad%e5%8f%a5/

(0)
AuroraAurora站点维系者
上一篇 2023年 12月 5日 下午6:03
下一篇 2023年 12月 5日 下午6:05

相关推荐

  • 介绍和安装

    文章目录介绍和安装什么是Golang为什么选择Golang选择Go时的一些优点安装苹果系统Windows 系统Linux 系统环境配置linux1.下载安装包 or 直接安装2.解…

    2023年 12月 5日
  • getsebool

    文章目录getsebool补充说明语法选项实例 getsebool 查询SElinux策略内各项规则的布尔值 补充说明 getsebool命令 是用来查询SElinux策略内各项规…

    入门教程 2023年 12月 14日
  • talk

    文章目录talk补充说明语法参数实例 talk 让用户和其他用户聊天 补充说明 talk命令 是talk服务器的客户端工具,通过talk命令可以让用户和其他用户聊天。linux中t…

    入门教程 2024年 3月 11日
  • dpkg-query

    文章目录dpkg-query补充说明语法选项参数实例 dpkg-query Debian Linux中软件包的查询工具 补充说明 dpkg-query命令 是Debian Linu…

    入门教程 2023年 12月 7日
  • init

    文章目录init补充说明语法选项参数实例 init init进程是所有Linux进程的父进程 补充说明 init命令 是Linux下的进程初始化工具,init进程是所有Linux进…

    入门教程 2023年 12月 19日
  • pwconv

    文章目录pwconv补充说明语法实例 pwconv 用来开启用户的投影密码 补充说明 pwconv命令 用来开启用户的投影密码。Linux系统里的用户和群组密码,分别存放在名称为p…

    入门教程 2024年 3月 1日
  • ipcs

    文章目录ipcs补充说明语法选项资源选项输出选项通用选项实例相关命令 ipcs 分析消息队列共享内存和信号量 补充说明 ipcs命令 用于报告Linux中进程间通信设施的状态,显示…

    入门教程 2023年 12月 19日
  • dirs

    文章目录dirs语法主要用途选项参数返回值例子注意 dirs 显示目录堆栈。 语法 dirs [-clpv] [+N] [-N] 主要用途 显示目录堆栈。 清空目录堆栈。 选项 -…

    入门教程 2023年 12月 7日
  • ifup

    文章目录ifup补充说明语法参数实例 ifup 激活指定的网络接口 补充说明 ifup命令 用于激活指定的网络接口。 语法 ifup(参数) 参数 网络接口:要激活的网络接口。 实…

    入门教程 2023年 12月 19日
  • mke2fs

    文章目录mke2fs补充说明语法选项参数实例 mke2fs 创建磁盘分区上的“etc2/etc3”文件系统 补充说明 mke2fs命令 被用于创建磁盘分区上的“etc2/etc3”…

    入门教程 2024年 1月 3日

发表回复

登录后才能评论
Translate »