diff --git a/README.md b/README.md index a447f6e..f8c1e2b 100644 --- a/README.md +++ b/README.md @@ -10,4 +10,4 @@ + [基础语法与环境搭建](./day01/README.md) + [复合数据结构](./day02/README.md) + [函数与结构体](./day03/README.md) -+ [结构体(2)与指针](./day04/README.md) \ No newline at end of file ++ [结构体(2)与指针与条件语句](./day04/README.md) \ No newline at end of file diff --git a/day04/README.md b/day04/README.md index 4eaed32..847319c 100644 --- a/day04/README.md +++ b/day04/README.md @@ -2,4 +2,5 @@ + [Go语言结构体](./struct/README.md) + [Go语言指针】【](./pointer/README.md) -+ [扩展: Go语言反射](./reflect/README.md) \ No newline at end of file ++ [扩展: Go语言反射](./reflect/README.md) ++ [条件语句](./condition/README.md) \ No newline at end of file diff --git a/day04/condition/README.md b/day04/condition/README.md new file mode 100644 index 0000000..dfce665 --- /dev/null +++ b/day04/condition/README.md @@ -0,0 +1,130 @@ +# 编程 分支模型(条件选择) + + +## 单分支 + +```go +if 布尔表达式 { + /* 在布尔表达式为 true 时执行 */ +} else { + /* 在布尔表达式为 false 时执行 */ +} +``` + +```go +// 单分支 +// 判断分数 及格还是不及格 +var score int = 59 +if score >= 60 { + println("及格") +} else { + println("不及格") +} +``` + +## 多分支 + +```go +if 布尔表达式 1 { + /* 在布尔表达式 1 为 true 时执行 */ +} else if 布尔表达式 2 { + /* 在布尔表达式 1 为 true 时执行 */ +} else if 布尔表达式 n { + /* 在布尔表达式 1 为 true 时执行 */ +} else { + /* 以上条件都不满足时执行 */ +} +``` + +```go +score = 85 +// 多分支 +// 判断分数 优秀 良好 中等 及格 不及格 +if score >= 90 { + println("优秀") +} else if score >= 80 { + println("良好") +} else if score >= 70 { + println("中等") +} else if score >= 60 { + println("及格") +} else { + println("不及格") +} +``` + +## 嵌套 + +```go +// 身高1.8m以上, 25 ~ 35岁, 男 +func (p *Person) Passed() bool { + if p.height > 1.8 { + if p.age > 25 && p.age <= 35 { + if p.gender == "male" { + return true + } + } + } + return false +} +``` + +## 多分支优化语法(switch case) + +多分支模型的优化语法 + +```go +score = 85 +// 多分支 +// 判断分数 优秀 良好 中等 及格 不及格 +if score >= 90 { + println("优秀") +} else if score >= 80 { + println("良好") +} else if score >= 70 { + println("中等") +} else if score >= 60 { + println("及格") +} else { + println("不及格") +} +``` + +```go +// switch 多分支 进行重写 +switch { +case score >= 90: + println("优秀") +case score >= 80: + println("良好") +case score >= 70: + println("中等") +case score >= 60: + println("及格") +default: + println("不及格") +} +``` + +## 多分支 并行执行 + +```go +// switch 多分支 进行重写 +switch { +case score >= 90: + println("优秀") +case score >= 80: + println("良好") + fallthrough +case score >= 70: + println("中等") +case score >= 60: + println("及格") + fallthrough +default: + println("不及格") +} +``` + + + diff --git a/day04/condition/main.go b/day04/condition/main.go new file mode 100644 index 0000000..364075e --- /dev/null +++ b/day04/condition/main.go @@ -0,0 +1,79 @@ +package main + +import "fmt" + +func main() { + // 单分支 + // 判断分数 及格还是不及格 + var score int = 59 + if score >= 60 { + println("及格") + } else { + println("不及格") + } + + score = 85 + // 多分支 + // 判断分数 优秀 良好 中等 及格 不及格 + if score >= 90 { + println("优秀") + } else if score >= 80 { + println("良好") + } else if score >= 70 { + println("中等") + } else if score >= 60 { + println("及格") + } else { + println("不及格") + } + + // switch 多分支 进行重写 + switch { + case score >= 90: + println("优秀") + case score >= 80: + println("良好") + fallthrough + case score >= 70: + println("中等") + case score >= 60: + println("及格") + fallthrough + default: + println("不及格") + } + + // 练习题 + // 身高1.8m以上, 25 ~ 35岁, 男 + // 则恭喜你通过面试 否则 谢谢参与 + + p := Person{ + height: 1.9, + age: 30, + gender: "male", + } + + if p.Passed() { + fmt.Println("congratulations! your successed") + } else { + fmt.Println("not passed") + } +} + +type Person struct { + height float32 + age uint + gender string +} + +// 身高1.8m以上, 25 ~ 35岁, 男 +func (p *Person) Passed() bool { + if p.height > 1.8 { + if p.age > 25 && p.age <= 35 { + if p.gender == "male" { + return true + } + } + } + return false +}