Program flow control
decide how to implement the program, the three common flow control statements
- Sequence control
- Branch control: if-else
- Loop control: for compliance with the conditions before the loop control, loop control (note 10 to the record) after qualifying
Sequence control
-
From top to bottom execution, each program follow this principle, in accordance with the implementation of a section of the line, no judgment, until the program is finished or half-way error.
-
About variables, we must follow the principle of forward references lawful {}, which is to declare re-use, not to use, re-statement
package main import "fmt" func main() { //正确 var a int = 100 a += 100 fmt.Println(a) //错误 b += 100 var b int = 200 fmt.Println(b) }
if-else if-else branch control
- if-else: single branch, branch double, multiple three branches
- Single branches : the establishment of the code block is executed when the condition is determined, the skipped block is not satisfied, no output. if <conditional expression> {<codes contents>}
- Dual branch : the establishment of a code block is executed, the block of code is not satisfied 2, comes under the control condition continues execution order determination. if <conditional expression> {<Code Content 1>} else {<Code Description 2>}
- Multibranched : 1 block of code is set up, does not hold the block of code is executed does not hold 2 ... block n, else not set up in the code block is executed when the condition is determined.
if <conditional expression> {<Code Content 1>} else if <conditional expression> {<Code Description 2>} ... else if <conditional expression> {<Code Contents n>} else {<final code contents>}
-
About if format, the official suggested expressions without parentheses, and all development code does not add the expression in parentheses, but the parentheses add nothing wrong
if a>10 { fmt.Println("a大于10") //a>10执行打印,a<=10 }
-
About if-else format, Golang provisions only way to write:
if a>10 { fmt.Println("a大于10") } else { fmt.Println("a小于10") }
-
About if- else if - else, else it is not required, once a set up, then skip to the bottom of the code sequence control
if a>10 { fmt.Println("a大于10") } else if a==10 { fmt.Println("a等于10") } else { fmt.Println("a小于10") }
-
Use the pit: must be added back if the conditional expression, can be a == 10, but can not be a = 10 this assignment, the compiler is certainly not pass
Multibranched Example:
High Schools through ax ^ 2 + bx + c = 0 are input variables a, b, c of three parameters, there are several root of the equation is determined
b ^ 2 - 4ac> 0: There are two distinct real roots
b ^ 2 - 4ac = 0: there is one real
b ^ 2 - 4ac <0: no real roots
package main
import (
"fmt"
"math"
)
func main() {
/*
需求分析:
中学数学学过 ax^2+bx+c=0 分别输入变量a,b,c三个参数,判断方程有几个根
b^2 - 4ac > 0:有两个不同实根
b^2 - 4ac = 0:有一个实根
b^2 - 4ac < 0:无实根
引入x1和x2两个变量接收 b^2 - 4ac > 0:有两个不同实根情况
x1= (-b + math.Sqrt(b^2 - 4ac))/2a
x2= (-b - math.Sqrt(b^2 - 4ac))/2a
引入变量x一个变量接收 b^2 - 4ac = 0:有一个实根情况
x = -b / 2a
如果无实根则打印,没有实根
*/
var (
a float64
b float64
c float64
drt float64
)
fmt.Printf("请分别按顺序输入方程中a,b,c三个参数数值,用空格间隔开:\n")
fmt.Scanf("%f %f %f",&a,&b,&c)
drt = float64(b*b - 4 * a * c)
if drt > 0 {
x1 := (-b + math.Sqrt(drt)) / 2 * a
x2 := (-b - math.Sqrt(drt)) / 2 * a
fmt.Printf("\n该方程有两个实根,分别是\nx1=%v, x2=%v",x1,x2)
} else if drt == 0 {
x := -b / 2 * a
fmt.Printf("该方程有一个实根,分别是 x=%v",x)
} else if drt < 0 {
fmt.Printf("该方程没有实根")
}
}
Nested branches: branch nested new branch, that is, if nested if, iteration
Multi-branch nested case presentations:
package main
import "fmt"
func main() {
/*
需求:圣斗士可进入竞技场的条件,
战斗力大于等于1000的为黄金圣斗士;
战斗力大于等于500小于1000的为白银圣斗士;
战斗力大于等于200小于500的为青铜圣斗士;
战斗力小于200的为战五渣,直接出局,不可进入竞技场
需求分析:
进入竞技场和进不了竞技场为两类人,需要用if-else判断;
if条件成立则
进入竞技场的又可以继续划分圣斗士等级,使用if-else if - else结构
if条件不成立则直接让战五渣out
*/
var name string
var power int
fmt.Println("请输入圣斗士姓名: ")
fmt.Scanf("%s",&name)
fmt.Printf("请输入圣斗士%s的战斗力:",name)
fmt.Scanf("%d",&power)
if power > 200 {
fmt.Printf("圣斗士%s允许进入竞技场参加角逐",name)
if power >= 1000 {
fmt.Printf("圣斗士%s经过战力评定判定为黄金圣斗士",name)
} else if power >= 500 {
fmt.Printf("\n圣斗士%s经过战力评定判定为白银圣斗士",name)
} else if power > 200 {
fmt.Printf("\n圣斗士%s经过战力评定判定为青铜圣斗士",name)
}
} else if power <= 200 && power >5 {
fmt.Printf("圣斗士%s战斗力不足,不允许进入竞技场",name)
} else {
fmt.Printf("圣斗士%s是战斗力不足5的渣,不允许进入竞技场",name)
}
}
branch control switch
-
switch statement based on different conditions to perform different actions, each case branch is unique, individually tested from top to bottom until a match.
-
Back matches ★★★ Golang language not need to add break! ! ! !
-
Multiple selection switch particularly suitable manner to get the corresponding results
-
After ★★★ switch contact and the case is an expression may be constants, variables, operators, there is a return value of the function , may be used.
-
★★★ case the value of the expression to be consistent with the value of the data type of the switch expression!
-
Behind the case can have multiple expressions, but all expressions must match the data type of switch!
-
All expressions in case if it is a constant value, can not be repeated, must be mutually exclusive!
-
default statement is not necessary, if there is no match switch directly to an end.
-
Back switch may not add expression, directly beneath the case as if-else if the branch to use
switch { case <分支表达式1>,<分支表达式2>: 语句块1 }
-
behind the switch can be a variable assignment expressions, switch num: = xxx {...}
-
switch to penetrate: fallthrough, namely case1 matches, logically speaking the end of the switch block, but I do not want to switch over, then use the fallthrough, the following case continues to match, fallthrough default can only penetrate a layer, if the following case2 also match the criteria, case1, case2 will output
If the comparison and switch
- If it is judged not more specific values, in line with integer, floating-point, character, character string types that use efficient switch.
- And the judgment section judges the type of bool, if better use
Code Example:
switch <表达式>(多用来传入变量,不然没有意义) {
case <分支表达式1>,<分支表达式2>:
语句块1 //没有break,默认有符合的case执行完了默认就break了,结束switch块
case <分支表达式3>,<分支表达式4>...<分支表达式n>:
语句块2
.
.
.
default:
<上面都不匹配>语句块d //类似异常执行后的finally
} //待完成之后结束switch块继续执行顺序控制
Case:
package main
import "fmt"
func main() {
/*
需求:冰封王座英雄技能表,首先输入一个英雄名称,然后查看英雄相应技能及说明
英雄名称:恶魔猎手,死亡骑士
恶魔猎手技能:法力燃烧,献祭,闪避,恶魔化身
死亡骑士技能:死亡缠绕,吃尸,邪恶光环,黑暗奴仆
*/
var heroName string
fmt.Println("请输入要查询的英雄的名字:")
fmt.Scanf("%v",&heroName)
switch heroName {
case "恶魔猎手":
var skill string
fmt.Println("请输入要查询的技能名字:")
fmt.Scanf("%v",&skill)
switch skill {
case "法力燃烧":
fmt.Printf("%s真的很牛逼啊~", skill)
case "献祭":
fmt.Printf("%s真的很牛逼啊~", skill)
case "闪避":
fmt.Printf("%s真的很牛逼啊~", skill)
case "恶魔化身":
fmt.Printf("%s真的很牛逼啊~", skill)
}
case "死亡骑士":
var skill string
fmt.Println("请输入要查询的技能名字:")
fmt.Scanf("%v",&skill)
switch skill {
case "死亡缠绕":
fmt.Printf("%s真的很牛逼啊~",skill)
case "吃尸":
fmt.Printf("%s真的很牛逼啊~",skill)
case "邪恶光环":
fmt.Printf("%s真的很牛逼啊~",skill)
case "黑暗奴仆":
fmt.Printf("%s真的很牛逼啊~",skill)
}
default:
fmt.Printf("没有查询到该英雄名称,请重新输入")
}
}