Go语言 流程控制

If / else分支判断

if condition1 {


}

 

if condition1 {
    
} else {

}

 

if condition1 {
    
} else if condition2 {

} else if condition3 {
} else {
}  

练习

package main
import “fmt”
func main() {
    bool1 := true
    if bool1 {
        fmt.Printf(“The value is true\n”)
    } else {
        fmt.Printf(“The value is false\n”)
    }
}  

练习

// 写一个程序, 从终端读取输入, 并转成整数. 如果转成整数出错,
// 则输出'can not convert to int', 并返回.否则输出该整数

package main

import (
    "fmt"
    "strconv"
)

func main() {
    var s string
    fmt.Scanf("%s", &s)
    res, err := strconv.Atoi(s)
    if err != nil {
        fmt.Println("can not convert to int.err: ", err)
        return
    }
    fmt.Println(res)

}

switch case语句

写法1

switch var {
case var1:
case var2:
case var3:
default:
}

 

写法2

var i = 0
switch i {
case 0:
case 1:
      fmt.Println(“1”)
case 2:
    fmt.Println(“2”)
default:
     fmt.Println(“def”)
}

 

写法3

fallthrough:Go里面switch默认相当于每个case最后带有break,匹配成功后不会自动向下执行其他case,而是跳出整个switch, 但是可以使用fallthrough强制执行下面的的case代码。

var i = 0
switch i {
case 0:
        fallthrough
case 1:
      fmt.Println(“1”)
case 2:
fmt.Println(“2”)
default:
     fmt.Println(“def”)
}

  

写法4

var i = 0
switch i {
case 0, 1:
      fmt.Println(“1”)
case 2:
fmt.Println(“2”)
default:
     fmt.Println(“def”)
}

 

写法5

var i = 0
switch {
condition1:
      fmt.Println(“i > 0 and i < 10”)
condition2:
fmt.Println(“i > 10 and i < 20”)
default:
     fmt.Println(“def”)
}



var i = 0
switch {
case i > 0 && i < 10:
      fmt.Println(“i > 0 and i < 10”)
case i > 10 && i < 20:
fmt.Println(“i > 10 and i < 20”)
default:
     fmt.Println(“def”)
}

  

写法6

switch i := 0 {
case i > 0 && i < 10:
      fmt.Println(“i > 0 and i < 10”)
case i > 10 && i < 20:
fmt.Println(“i > 10 and i < 20”)
default:
     fmt.Println(“def”)
}

 

for 语句

写法1

for 初始化语句; 条件判断; 变量修改 {
}

for i := 0 ; i < 100; i++ {
fmt.Printf(“i=%d\n”, i)
}

 

写法2

for  条件 {
}


for i > 0 {
      fmt.Println(“i > 0”)
}

for true {
      fmt.Println(“i > 0”)
}

for {
      fmt.Println(“i > 0”)
}

 

写法3

for range 语句        用来遍历数组、slice、map、chan

str := “hello world,中国”
for i, v := range str {
     fmt.Printf(“index[%d] val[%c] len[%d]\n”, i, v, len([]byte(v)))
} 

 

写法4

break continue语句

str := “hello world,中国”
for i, v := range str {
       if i > 2 {
             continue
       }
  if (i > 3) {
         break  }
     fmt.Printf(“index[%d] val[%c] len[%d]\n”, i, v, len([]byte(v)))
}

 

写法5

goto 和 label 语句

package main
import "fmt"
func main() {
LABEL1:
	for i := 0; i <= 5; i++ {
		for j := 0; j <= 5; j++ {
			if j == 4 {
				continue LABEL1
			}
			fmt.Printf("i is: %d, and j is: %d\n", i, j)
		}
	}
}


  

package main

func main() {
	i := 0
HERE:
	print(i)
	i++
	if i == 5 {
		return
	}
	goto HERE
}

  

练习

i := 0for {
      if i >= 3 { 
   break 
   }

      fmt.Println(“”, i)
      i++;
}



for i := 0; i<7 ; i++ {       if i%2 == 0 { 
             continue 
        } 
         fmt.Println(i)
} 

  

 

posted @ 2019-08-29 10:31  离人怎挽_wdj  阅读(124)  评论(0编辑  收藏  举报