第 7 节:运算符流程控制

7:流程控制

前面我们写的程序都是从第一行开始执行,一直执行到末尾,一行一行的顺序执行下来,这种执行结构叫顺序执行结构。
GO语言除了有顺序结构,还有选择结构,循环结构。

  • 顺序结构:程序按顺序执行,不发生跳转。
  • 选择结构:依据是否满足条件,有选择的执行相应功能。
  • 循环结构:依据条件是否满足,循环多次执行某段代码。

下面先讲解选择结构:

1: 01if条件语句

package main

import (
    "fmt"
)

func main0101() {

    //if 表达式
    //{
    //代码体
    //}

    var score int

    fmt.Scan(&score)

    //if score > 700 {
    //  fmt.Println("我要上清华")
    //}

    //if score > 700 {
    //  fmt.Println("我要上清华")
    //} else {
    //  fmt.Println("我要上斯坦福")
    //}

    if score > 700 {
        fmt.Println("我要上清华")
    } else if score > 680 {
        fmt.Println("我要上北大")
    } else if score > 650 {
        fmt.Println("我要上人大")
    } else {
        fmt.Println("我要上斯坦福")
    }

    var m int

}

2:if语句嵌套

package main

import "fmt"

func main0201() {
    var score int

    fmt.Scan(&score)

    if score > 700 {
        fmt.Println("我要上清华")
        if score > 720 {
            fmt.Println("我要学挖掘机")
        } else if score > 710 {
            fmt.Println("我要学美容美发")
        } else {
            fmt.Println("我要学习汽修")
        }

    } else if score > 680 {
        fmt.Println("我要上北大")

        if score > 690 {
            fmt.Println("我要学盗墓")
        } else {
            fmt.Println("我要学习go语言")
        }
    }

}

//三只小猪称体重 通过键盘输入三只小猪体重 找到最重的
func main() {

    var a, b, c int
    //10 8 12
    fmt.Scan(&a, &b, &c)

    if a > b {
        //a重
        if a > c {
            fmt.Println("a最重")
        } else {
            fmt.Println("c最重")
        }
    } else {
        //b重
        if b > c {
            fmt.Println("b最重")
        } else {
            fmt.Println("c最重")
        }
    }
}
//根据分数 >=90 A >=80 B >=70 C >=60 D 不及格 E

3:switch分支语句

package main

import "fmt"

func main0301() {

    //switch 变量(表达式) {
    //case 值1:
    //  代码体
    //  fallthrough
    //case 值2:
    //  代码体
    //default:
    //  代码体
    //}

    //根据分数 >=90 A >=80 B >=70 C >=60 D 不及格 E

    var score int
    fmt.Scan(&score)
    switch score / 10 {
    case 10:
        //fmt.Println("A")
        fallthrough
    case 9:
        fmt.Println("A")
    case 8:
        fmt.Println("B")
    case 7:
        fmt.Println("C")
    case 6:
        fmt.Println("D")
    default:
        fmt.Println("E")
    }
}
func main0302() {

    var score int
    fmt.Scan(&score)

    switch {
    case score >= 90:
        fmt.Println("A")
    case score >= 80:
        fmt.Println("B")
    case score >= 70:
        fmt.Println("C")
    case score >= 60:
        fmt.Println("D")
    default:
        fmt.Println("E")

    }
}
func main0303() {
    var score int
    fmt.Scan(&score)
    switch score > 60 {
    case true:
        fmt.Println("及格")
    case false:
        fmt.Println("不及格")
    }
}

func main() {
    //根据输入的年份月份 计算这个月有多少天

    var y int
    var m int
    fmt.Scan(&y, &m)

    //在switch语句中可以把相同的值放在一个case中
    switch m {
    case 1, 3, 5, 7, 8, 10, 12:
        fmt.Println(31)
    case 4, 6, 9, 11:
        fmt.Println(30)
    //fallthrough 在case中向下执行下一个case
    //case 1:
    //  fallthrough
    //case 3:
    //  fallthrough
    //case 5:
    //  fallthrough
    //case 7:
    //  fallthrough
    //case 8:
    //  fallthrough
    //case 10:
    //  fallthrough
    //case 12:
    //  fmt.Println(31)
    //
    //case 4:
    //  fallthrough
    //case 6:
    //  fallthrough
    //case 9:
    //  fallthrough
    //case 11:
    //  fmt.Println(30)
    case 2:
        //判断是否是闰年  能被4整除 但是 不能被100整除  或 能被400整除
        if y%4 == 0 && y%100 != 0 || y%400 == 0 {
            fmt.Println(29)
        } else {
            fmt.Println(28)
        }

    default:
        fmt.Println("月份输入错误")
    }
}

//case 3:
//  fmt.Println(31)
//case 4:
//  fmt.Println(30)
//case 5:
//  fmt.Println(31)
//case 6:
//  fmt.Println(30)
//case 7:
//  fmt.Println(31)
//case 8:
//  fmt.Println(31)
//case 9:
//  fmt.Println(30)
//case 10:
//  fmt.Println(31)
//case 11:
//  fmt.Println(30)
//case 12:
//  fmt.Println(31)

4: if和switch比较

package main

func main() {

    //优点
    //if 可以进行区间判断 嵌套使用
    //switch 执行效率高 可以将多个满足相同条件的值放在一起

    //缺点
    //if 执行效率低
    //switch 不建议嵌套使用
}

5: 循环语句

package main

import "fmt"

func main() {

    //for i := 0; i < 5; i++ {
    //  fmt.Println("6666")
    //}

    //for i := 1; i <= 10; i++ {
    //  fmt.Println(i)
    //}

    //计算1-100和
    //sum := 0
    //for i := 1; i <= 100; i++ {
    //  sum += i
    //}
    //
    //fmt.Println(sum)

    //计算1-100偶数的和
    //在for语句中嵌套if条件判断
    //sum := 0
    //for i := 1; i <= 100; i++ {
    //  if i%2 == 0 {
    //      sum+=i
    //  }
    //}
    //
    //fmt.Println(sum)
    //计算1-100偶数的和
    sum := 0
    for i := 0; i <= 100; i += 2 {
        sum += i
    }

    fmt.Println(sum)

}
本作品采用《CC 协议》,转载必须注明作者和本文链接
嗨,我是波波。曾经创业,有收获也有损失。我积累了丰富教学与编程经验,期待和你互动和进步! 公众号:上海PHP自学中心 付费知识星球:破解面试:程序员的求职导师
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!
司机 @ 欣昊玉
文章
273
粉丝
339
喜欢
558
收藏
1106
排名:64
访问:12.2 万
私信
所有博文
社区赞助商