Go循环学习

if 语句

func main() {

	var score int
	println("输入你的分数")
	fmt.Scanln(&score)
	// a b c d
	if score >= 90 && score <= 100 {
		fmt.Println("A")
	} else if score >= 80 && score < 90 {
		fmt.Println("B")
	} else if score >= 70 && score < 80 {
		fmt.Println("C")
	} else {
		fmt.Println("D")
	}

}

if多嵌套

func main() {
	var a, b int
	var pwd int = 20221031
	fmt.Scanln(&a)

	if a == pwd {
		fmt.Println("请再次输入密码")
		fmt.Scanln(&b)
		if b == pwd {
			fmt.Println("注册成功")
		} else {
			fmt.Println("第二次密码和第一次不一样")
		}

	} else {
		fmt.Println("密码输入错误")
	}

}

switch语句

func main() {
	var score int
	println("输入你的分数")
	fmt.Scanln(&score)
	switch score {
	case 90:
		fmt.Println("A")
	case 80:
		fmt.Println("B")
	case 70, 60, 50:
		fmt.Println("C")
	default:
		fmt.Println("D")
	}
	//默认bool为true
	switch {
	case false:
		fmt.Println("false")
	case true:
		fmt.Println("true")
	default:
		fmt.Println("其他")
	}
}

fallthrough

func main() {
	a := false
	switch a {
	case false:
		fmt.Println("case的值为false")
		fallthrough //可以进行case的穿透 只穿透下一个case的值  下面break不让它穿了,很少用,还是要知道的

	case true:
		if a == false {
			break
		}
		fmt.Println("case的值为true")

	}

}

for循环

func main() {
	//for 条件的起始值;循环条件;控制变量进行自增自减
	//for i := 1; i <= 10; i++ {
	//	fmt.Println(i)
	//}
	sum := 0
	for i := 1; i <= 10; i++ {
		sum = sum + i
	}
	fmt.Println(sum)

}

打印5*5的方针

// 打印一个5*5方正
func main() {
	for j := 1; j <= 5; j++ {
		for i := 1; i <= 5; i++ {
			fmt.Print("*")
		}
		fmt.Println()
	}

}

打印9*9乘法表

func main() {
	for j := 1; j <= 9; j++ {
		for i := 1; i <= j; i++ {
			fmt.Printf("%d*%d=%d\t", i, j, i*j)
		}
		fmt.Println()
	}

}

break与continue

func main() {
	//break结束当前循环
	for i := 0; i < 10; i++ {
		if i == 5 {
			break
		}
		fmt.Println(i)
	}
	fmt.Println("=========================")
	//continue 结束当次循环
	for i := 0; i < 10; i++ {
		if i == 5 {
			continue
		}
		fmt.Println(i)
	}

}

遍历string

func main() {
	str := "hello,zhounan"
	fmt.Println(str)

	//获取字符串的长度
	fmt.Println("字符串的长度为", len(str))

	//获取指定的字节
	fmt.Println("字节打印", str[0])
	fmt.Printf("%c\n", str[0])
	for i := 0; i < len(str); i++ {
		fmt.Printf("%c", str[i])
	}
	fmt.Println()
	// for range循环,遍历数组,切片
	//返回下标和对应的值,使用这个值就可以了
	for i, v := range str {
		fmt.Print(i)
		fmt.Printf("%c", v)
	}
	//str[2]='A' string中是不能修改的 str[i]是type类型
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值