Go語言學習(四)-----分支、循環


在Go語言中只有很少的幾個控制結構,它沒有while或者do-while循環。

但是它有for、switch、if。而且switch接受像for那樣可選的初始化語句。下面來認識一下他們

一、if語句

Go語言中的if像下面這個樣子:

if x > 0 {
    return y
} else {
    return x
}

一般不需要加圓括號,不過,如果你寫上,也是沒有問題的(親測,寫上圓括號也可以的~~)。比如:

if (3>2) {
        fmt.Println("test if")
}else if true {
        fmt.Println("test else if")
}else{
        fmt.Println("test else")
}

//輸出 test if

 

二、switch語句

Go的switch非常靈活。表達式不必是常量或整數。
而在java中switch后面的括號里面只能放int類型的值,注意是只能放int類型,但是放byte,short,char類型的也可以,

那是因為byte,short,shar可以自動提升(自動類型轉換)為int,也就是說,你放的byte,short,shar類型,然后他們會自動轉換為int類型(寬化,自動轉換並且安全),
其實最后放的還是int類型!

switch語句執行的過程從上至下,直到找到匹配項,匹配項后面也不需要再加break(又跟java不一樣哦!)

func switchFuncString(a string) {
    //字符串
    switch a {
    case "test1":
        fmt.Println("test1")
    case "test2", "test3":
        fmt.Println("testOhter")
    default:
        fmt.Println("NoTest")
    }

}

func switchFuncInt(a int) {
    //數字
    switch a {
    case 1:
        fmt.Println("1")
    case 2:
        fmt.Println("2")
    case 3:
        fmt.Println("3")
    }
}

func switchFuncBool(c byte) {
    //switch后面什么都沒有?它會匹配true
    switch {
    case '0' <= c && c <= '9':
        fmt.Println(c - '0')
    case 'a' <= c && c <= 'f':
        fmt.Println( c - 'a' + 10)
    case 'A' <= c && c <= 'F':
        fmt.Println( c - 'A' + 10)
    }
}

但是如果,你就希望匹配之后,繼續匹配下面一條怎么辦呢?還是有辦法的,使用“fallthrough”即可,例如:

func switchFuncInt(a int) {
    //數字
    switch a {
    case 1:
        fmt.Println("1")
        fallthrough 
    case 2:
        fmt.Println("2")
    case 3:
        fmt.Println("3")
    }
}

調用switchFuncInt(1),打印出1和2來。

 

三、for循環

Go語言的For循環油3中形式,只有其中的一種使用分號。

  1. for init; condition; post { }          和C 的for 一樣
  2. for condition { }                         和while 一樣
  3. for { }                                      和C 的for(;;) 一樣(死循環)

直接上代碼~~

package main

import "fmt"

func main() {
    simpleFor()

    var test string = "asdfghjkl"
    fmt.Println(reverse([]byte(test)))
}

//打印0~9
func simpleFor() {
    for i := 0; i < 10; i++ {
        fmt.Println(i)
    }
}

// Reverse a
func reverse(a []byte) string {
    //由於Go沒有逗號表達式,而++和--是語句而不是表達式,
    //如果你想在for中執行多個變量,應當使用平行賦值。
    for i, j := 0, len(a)-1; i < j; i, j = i+1, j-1 {
        a[i], a[j] = a[j], a[i]
    }
    return string(a)
}

 

分支、循環是很基礎的東西,所以有必要多練練手~~

下面來做兩個題目玩玩~~

1. 創建一個基於for的簡單的循環。使其循環10次,並且使用fmt 包打印出計數器的值。
2. 用goto改寫1的循環。保留字for不可使用。
3. 再次改寫這個循環,使其遍歷一個array,並將這個array打印到屏幕上。

代碼:

View Code
 1 package main
 2 
 3 import "fmt"
 4 /**
 5  *打印0~9
 6  */
 7 func main() {
 8     printZero2Nine()
 9     printZero2NineByGoto()
10 
11     var a [10]int
12     for i:=0;i<10;i++{
13         a[i] = i
14     }
15     
16     //一句話就可以打印出數組里面的元素來
17     fmt.Printf("%v\n", a)
18     
19     printArrayByFor(a)
20     printArrayByGoto(a)
21 
22 }
23 
24 func printZero2Nine() {
25     for i := 0; i < 10; i++ {
26         fmt.Print(i)
27         fmt.Print("\t")
28     }
29     fmt.Println()
30 }
31 
32 func printZero2NineByGoto() {
33     i := 0
34 Label:
35     if i < 10 {
36         fmt.Print(i)
37         fmt.Print("\t")
38         i++
39         goto Label
40     }
41     fmt.Println()
42 }
43 
44 func printArrayByFor(a [10]int) {
45     //用for循環來遍歷數組
46     for i := 0; i < len(a); i++ {
47         fmt.Print(a[i])
48         fmt.Print("\t")
49     }
50     fmt.Println()
51 }
52 
53 func printArrayByGoto(a [10]int) {
54     i := 0
55 Label:
56     if i < len(a) {
57         fmt.Print(a[i])
58         fmt.Print("\t")
59         i++
60         goto Label
61     }
62     fmt.Println()
63 }

 

Go,Go,Go,明天繼續...

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM