栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

go语言的三种流控制语句(if,for,switch)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

go语言的三种流控制语句(if,for,switch)

if语句
    if score:=100;score > 90{
		fmt.Println("优秀")
	}else if score > 80{
		fmt.Println("普通")
	}else {
		fmt.Println("差劲")
	}

在if语句中可以定义变量并使用。

Switch语句
	score := 100
	switch  {
	case score>90:
		fmt.Println("优秀")
	case score>80:
		fmt.Println("一般")
	case score>70:
		fmt.Println("差劲")
	default:
		fmt.Println("不知道怎么说")
	}

	level := 3
	switch level {
	case 3:
		fmt.Println("优秀")
	case 2:
		fmt.Println("一般")
	case 1:
		fmt.Println("差劲")
	default:
		fmt.Println("不知道怎么说")
	}

两种不同的switch方式,与c语言不同的是每个case里面不用break

for语句
    var nums [10]int
	for i:=0;i<10;i++{
		nums[i]=i+100
	}
	for index,value := range nums{
		fmt.Printf("nums[%d]=%dn",index,value)
	}
	x := 0
	for x<5{
		fmt.Println(x)
		x++
	}

使用range搭配for是一种比较常见的做法。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/352484.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号