• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

go流程控制

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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 := 10
 if num % 2 == 0 { //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 := 10; num % 2 == 0 { //checks if number is even
 fmt.Println(num,"is even")
 } else {
 fmt.Println(num,"is odd")
 }
}

练习三

package main
import ( 
 "fmt"
)
func main() { 
 num := 99
 if num <= 50 {
   fmt.Println("number is less than or equal to 50")
 } else if num >= 51 && num <= 100 {
   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 := 1; i <= 10; i++ {
   fmt.Printf(" %d",i)
 }
}

break,终止循环

package main
import ( 
 "fmt"
)
func main() { 
 for i := 1; i <= 10; i++ {
   if i > 5 {
     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 := 1; i <= 10; i++ {
    if i%2 == 0 {
       continue
    }
    fmt.Printf("%d ", i)
   }
}

省略写法

package main
import ( 
 "fmt"
)
func main() { 
  i := 0
  for ;i <= 10; { // initialisation and post are omitted
    fmt.Printf("%d ", i)
    i += 2
  }
}

 

package main
import ( 
 "fmt"
)
func main() { 
  i := 0
  for i <= 10 { // initialisation and post are omitted
    fmt.Printf("%d ", i)
    i += 2
  }
}

练习

package main
import ( 
 "fmt"
)
func main() { 
  for no, i := 10, 1; i <= 10 && no <= 19; i, no = i+1, no+1 {
    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 := 4
  switch finger {
    case 1:
      fmt.Println("Thumb")
    case 2:
      fmt.Println("Index")
    case 3:
      fmt.Println("Middle")
    case 4:
      fmt.Println("Ring")
    case 5:
      fmt.Println("Pinky")
    }
}

Switch default

 

package main
import ( 
 "fmt"
)
func main() { 
  switch finger := 8; finger {
  case 1:
    fmt.Println("Thumb")
  case 2:
    fmt.Println("Index")
  case 3:
    fmt.Println("Middle")
  case 4:
    fmt.Println("Ring")
  case 5:
    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 := 75
  switch { // expression is omitted
  case num >= 0 && num <= 50:
    fmt.Println("num is greater than 0 and less than 50")
  case num >= 51 && num <= 100:
    fmt.Println("num is greater than 51 and less than 100")
  case num >= 101:
    fmt.Println("num is greater than 100")
  }
}

Switch fallthrough 

带有 fallthrough 的case语句执行玩本条语句后继续向下执行

package main
import ( 
 "fmt"
)
func number() int { 
 num := 15 * 5
 return num
}
func main() {
  switch num := number(); { //num is not a constant
  case num < 50:
    fmt.Printf("%d is lesser than 50\n", num)
    fallthrough
  case num < 100:
    fmt.Printf("%d is lesser than 100\n", num)
    fallthrough
  case num < 200:
    fmt.Printf("%d is lesser than 200", num)
  }
}

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Server returns invalid timezone. Go to ‘Advanced‘ tab and set ‘serverTimezon ...发布时间:2022-07-10
下一篇:
go 内建变量类型发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap