```
docs(README): 更新目录结构添加条件语句章节 - 在主README中更新day04标题为"结构体(2)与指针与条件语句" - 在day04目录下添加条件语句相关文档链接 - 整理并完善第四天学习内容的导航结构 ```
This commit is contained in:
parent
11aaa2d03a
commit
db3b69e29b
@ -10,4 +10,4 @@
|
||||
+ [基础语法与环境搭建](./day01/README.md)
|
||||
+ [复合数据结构](./day02/README.md)
|
||||
+ [函数与结构体](./day03/README.md)
|
||||
+ [结构体(2)与指针](./day04/README.md)
|
||||
+ [结构体(2)与指针与条件语句](./day04/README.md)
|
||||
@ -3,3 +3,4 @@
|
||||
+ [Go语言结构体](./struct/README.md)
|
||||
+ [Go语言指针】【](./pointer/README.md)
|
||||
+ [扩展: Go语言反射](./reflect/README.md)
|
||||
+ [条件语句](./condition/README.md)
|
||||
130
day04/condition/README.md
Normal file
130
day04/condition/README.md
Normal file
@ -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("不及格")
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
79
day04/condition/main.go
Normal file
79
day04/condition/main.go
Normal file
@ -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
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user