首页 技术 正文
技术 2022年11月15日
0 收藏 718 点赞 4,046 浏览 2777 个字

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)
  }
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,493
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295