Go的条件判断语句和跳转关键字break,continue,go

2017-09-28  本文已影响0人  Link_Biao

if判断语句

func tempIf(a int) {
    if a > 0 {
        f.Println("if is true")
    } else {
        f.Println("if is false")
    }
}

for判断语句

func tempFor(a int) {//常用的循环模式
    for i := 0; i < 10; i++ {
        if a == i {
            f.Println("break")
            break;
        } 
    }
}
func tempFor1(a int) {
    for a!=10{
        f.Println("continue")
    }
}
func tempFor2(a int) {//类似while
    for{
        if a>5{
            f.Println(a)
            break;
        }
    }
}

switch判断语句

func tempSwitch(str string) {
    switch str {
    case "1":
        f.Println("str is 1")
        fallthrough//关键字,条件成立,依旧执行下一条case,不管下一条条件是否成立
    case "2":
        f.Println("str is 2")
    case "3":
        f.Println("str is 3")
    }
}

break关键字

LABLE1:
for {
    for i := 1; i < 10; i++ {
        if i == 4 {
            break LABLE1 //跳出定义了标签层
        }
    }
}
fm.Println("break is over")

goto关键字

for {
    for i := 1; i < 10; i++ {
        if i == 4 {
            goto LABLE2 //调整了执行位置,如果是标签是放在for语句
  //之前,又是重新开始执行
        }
    }
}
LABLE2:
fm.Println("goto is over")

continue关键字

LABLE3:
for i := 1; i < 10; i++ {
    for {
        fm.Print(i)
        continue LABLE3 //继续执行定义了标签层
    }
}
fm.Println("continue is over")

自增符号++和自减符号--

上一篇 下一篇

猜你喜欢

热点阅读