if else 语句
基本语法
if condition {
//do something
}if condition {
//do something
} else if condition {
//do something
} else {}
if statement; condition {
//do something
}
练习一
package main
import (
"fmt"
)
func main() {
num :=
if num % == { //checks if number is even
fmt.Println("the number is even")
} else {
fmt.Println("the number is odd")
}
}
练习二
package main
import (
"fmt"
)
func main() {
if num := ; num % == { //checks if number is even
fmt.Println(num,"is even")
} else {
fmt.Println(num,"is odd")
}
}
练习三
package main
import (
"fmt"
)
func main() {
num :=
if num <= {
fmt.Println("number is less than or equal to 50")
} else if num >= && num <= {
fmt.Println("number is between 51 and 100")
} else {
fmt.Println("number is greater than 100")
}
}
循坏
Go语言中只有一种循环 for
for initialisation; condition; post {
}
练习一
package main
import (
"fmt"
)
func main() {
for i := ; i <= ; i++ {
fmt.Printf(" %d",i)
}
}
break,终止循环
package main
import (
"fmt"
)
func main() {
for i := ; i <= ; i++ {
if i > {
break //loop is terminated if i > 5
}
fmt.Printf("%d ", i)
}
fmt.Printf("\nline after for loop")
}
continue 终止本次循坏
package main
import (
"fmt"
)
func main() {
for i := ; i <= ; i++ {
if i% == {
continue
}
fmt.Printf("%d ", i)
}
}
省略写法
package main
import (
"fmt"
)
func main() {
i :=
for ;i <= ; { // initialisation and post are omitted
fmt.Printf("%d ", i)
i +=
}
}
package main
import (
"fmt"
)
func main() {
i :=
for i <= { // initialisation and post are omitted
fmt.Printf("%d ", i)
i +=
}
}
练习
package main
import (
"fmt"
)
func main() {
for no, i := , ; i <= && no <= ; i, no = i+, no+ {
fmt.Printf("%d * %d = %d\n", no, i, no*i)
}
}
无限循环
package main
import (
"fmt"
)
func main() {
for {
fmt.Printf("hello")
}
}
switch 语句
switch
package main
import (
"fmt"
)
func main() {
finger :=
switch finger {
case :
fmt.Println("Thumb")
case :
fmt.Println("Index")
case :
fmt.Println("Middle")
case :
fmt.Println("Ring")
case :
fmt.Println("Pinky")
}
}
Switch default
package main
import (
"fmt"
)
func main() {
switch finger := ; finger {
case :
fmt.Println("Thumb")
case :
fmt.Println("Index")
case :
fmt.Println("Middle")
case :
fmt.Println("Ring")
case :
fmt.Println("Pinky")
default: //default case
fmt.Println("incorrect finger number")
}
}
case多个值
package main
import (
"fmt"
)
func main() {
letter := "i"
switch letter {
case "a", "e", "i", "o", "u":
fmt.Println("vowel")
default:
fmt.Println("not a vowel")
}
}
Switch case 条件判断
package main
import (
"fmt"
)
func main() {
num :=
switch { // expression is omitted
case num >= && num <= :
fmt.Println("num is greater than 0 and less than 50")
case num >= && num <= :
fmt.Println("num is greater than 51 and less than 100")
case num >= :
fmt.Println("num is greater than 100")
}
}
Switch fallthrough
带有 fallthrough 的case语句执行玩本条语句后继续向下执行
package main
import (
"fmt"
)
func number() int {
num := *
return num
}
func main() {
switch num := number(); { //num is not a constant
case num < :
fmt.Printf("%d is lesser than 50\n", num)
fallthrough
case num < :
fmt.Printf("%d is lesser than 100\n", num)
fallthrough
case num < :
fmt.Printf("%d is lesser than 200", num)
}
}