if-else,switch,for循环

if-else语句

//if-else
package main

func main() {
	//a:=9
	//1 基本使用
	//if a>10{
	//	fmt.Println("大于10")
	//}else {
	//	fmt.Println("小于等于10")
	//}

	//2 if -else if -else
	//if a>10{
	//	fmt.Println("大于10")
	//}else if a==10 {
	//	fmt.Println("等于10")
	//}else {
	//	fmt.Println("小于10")
	//}

	//3 不能换行(go语言每一行结尾,需要加一个;  ,每当换行,会自动加;)
	//if a>10{
	//	fmt.Println("大于10")
	//}else if a==10 {
	//	fmt.Println("等于10")
	//}else
	//{
	//	fmt.Println("小于10")
	//}

	//4 条件里可以进行初始化操作(有作用域范围的区别)
	//a:=10;
	//if a<10{
	//if a:=10;a<10{
	//	fmt.Println("xxx")
	//}else {
	//	fmt.Println("yyyy")
	//}
	//fmt.Println(a)

	//fmt.Println(a)
}

switch语句

//switch多条件选择

package main

import "fmt"

func main() {
	//1 switch基本使用
	//a:=10
	//switch a {
	//case 1:
	//	fmt.Println("1")
	//case 2:
	//	fmt.Println("2")
	//case 9:
	//	fmt.Println("9")
	//case 10:
	//	fmt.Println("10")
	//
	//}

	//2 default的基本使用
	//a:=16
	//switch a {
	//case 1:
	//	fmt.Println("1")
	//case 2:
	//	fmt.Println("2")
	//case 9:
	//	fmt.Println("9")
	//case 10:
	//	fmt.Println("10")
	//default:
	//	fmt.Println("不知道")
	//
	//}

	//3 多条件
	//a:=16
	//switch a {
	//case 1,2,3:
	//	fmt.Println("1")
	//case 4,5,6:
	//	fmt.Println("2")
	//case 7,9:
	//	fmt.Println("9")
	//case 10,16:
	//	fmt.Println("10")
	//default:
	//	fmt.Println("不知道")
	//
	//}


	////4 无表达式
	//a:=3
	//switch  {
	//case a==1 || a==3:
	//	fmt.Println("1")
	//case a==4 || a==5:
	//	fmt.Println("2")
	//default:
	//	fmt.Println("不知道")
	//
	//}

	//5 fallthrough,无条件执行下一个case
	a:=3
	switch  {
	case a==1 || a==3:
		fmt.Println("1")
		fallthrough //无条件执行下一个case
	case a==4 || a==5:
		fmt.Println("2")
	default:
		fmt.Println("不知道")

	}


}

for循环

//循环 (只有for循环 没有while 以及 do while)
package main

import "fmt"

func main() {
	//1 基本语法
	//for 初始化;条件判断;自增/自减{循环体内容}  三部分都可以省略

	//for i:=0;i<10;i++ {
	//	fmt.Println(i)
	//}

	//2 省略第一部分(初始化)(注意:作用域范围就不太一样了)
	//i:=0
	//for ;i<10;i++ {
	//	fmt.Println(i)
	//}

	////3 省略第三部分
	//for i:=0;i<10; {
	//	i++ //写在前面是从1开始
	//	fmt.Println(i)
	//	//i++  //写在后面是从0开始
	//}

	//4 省略第一和第三部分(其实看起来不就是while循环吗)
	//i:=0
	//for i<10 {
	//	i++ //写在前面是从1开始
	//	fmt.Println(i)
	//	//i++  //写在后面是从0开始
	//}

	//5 for 条件 {}
	//for true{
	//	fmt.Println("1")
	//}

	//6 死循环
	//for {
	//	fmt.Println("1")
	//}

	//7 break和continue
	for {
		fmt.Println("1")
		break
		//continue
	}
}

posted @ 2020-04-21 22:32  alen_zhan  阅读(156)  评论(0编辑  收藏  举报
返回顶部