当前位置:网站首页>Go branch and loop
Go branch and loop
2022-07-02 03:54:00 【UPythonFish】
List of articles
go Branches and loops
if-else sentence
go Of if-else Judgment statement , and js It's like , The grammar is as follows
if condition {
} else if condition {
} else {
}
When condition When the conditions are met, execute { } The code in , Not satisfied with the implementation of else Back
func main() {
number := 55
if number >=90 && number <= 100 {
// The beginning of braces should be on the same line as the keyword
// go By default, no line ends with ; An error will be reported if the line changes
fmt.Println(" good ")
} else if number >= 60 && number <= 90 {
// else if There must be conditions behind
fmt.Println(" good ")
} else {
// else There can be no conditions behind
fmt.Println(" fail, ")
}
}
for Loop statement
for
yes Go The only loop statement in language .Go There are no other languages in the language, such as python Medium while loop .
complete for loop grammar
for initialisation; condition; post {
}
The initialization statement is executed only once . After loop initialization , Cycle conditions will be checked . If the result of the condition is true
, be {}
The inner loop body will execute , Then perform post sentence .post Statement will execute after each successful loop iteration . In execution post After the statement , The conditions will be checked again . If true
, Then the loop will continue to execute , otherwise for The cycle will end .( Translation notes : This is typical for Loop three expressions , The first is the initialization expression or assignment statement ; The second is the expression of cyclic condition determination ; The third is the loop variable correction expression , That is to say post )
for Cyclic 5 Method of use
1. Index based loop
func main() {
// Loop printing 0-9
for i := 0; i < 10; i++ {
fmt.Println(i)
}
}
2. External initialization
func main(){
i := 0 // i The scope of is expanded
for ; i<10; i++{
// ; Don't omit
fmt.Println(i)
}
}
3. The loop variable correction expression is written internally
func main(){
i := 0 // i The scope of is expanded
for ; i<10; {
// Write only conditions It can be omitted ;
fmt.Println(i)
i++
}
}
func main(){
i := 0 // i The scope of is expanded
for i<10 {
// Write only conditions It can be omitted ;
fmt.Println(i)
i++
}
}
4. Omit the cycle condition
func main(){
for ; ; {
// here ; You can omit it
fmt.Println(" Dead cycle ")
}
}
func main(){
for {
// here ; You can omit it
fmt.Println(" Dead cycle ")
break // You can add break Keyword to terminate , There are also continue keyword , Skip the following code directly , Execute next cycle
}
}
5. Iteration based loop
func main(){
var a = [3]int[1, 25, 34] // Array
for i,value := range a {
fmt.Println(i) // Indexes
fmt.Println(value) // value
}
}
go Kind of for Same cycle Like other languages , It can also be executed A nested loop , The following list is printed 9 x 9 Multiplication table
func main() {
for i := 1; i < 10; i++ {
for j := 1; j <= i; j++ {
fmt.Printf("%d x %d = %d ", j, i, i*j)
}
fmt.Println()
}
}
switch sentence
switch It's a conditional statement , Used to compare the value of an expression with a list of possible matches , And execute the corresponding code block according to the matching situation . It can be thought of as an alternative to multiple if else
The common way of clause .
// 1 Usage 1
name:="tom"
switch name {
case "lxx":
fmt.Println("lxx")
case "tom":
fmt.Println("tom")
}
// Usage mode 2 Without conditions
name:="tom"
switch {
case name=="lxx":
fmt.Println("lxx")
case name=="tom":
fmt.Println("tom")
}
// Usage mode 3 Multiple conditions
name:="tom"
switch name {
case "lxx","jason": // perhaps
fmt.Println("lxx")
case "tom","marry":
fmt.Println("tom")
}
name := "tom"
switch {
case name == "lxx" || name == "jason": // perhaps
fmt.Println("lxx")
case name == "tom", name == "marry":
fmt.Println("tom")
}
// 4 The way 4 ,default Use , Does not meet all case after , perform
name:="tom"
switch name {
case "lxx":
fmt.Println("lxx")
case "tom":
fmt.Println("tom")
default:
fmt.Println(" I don't know who it is ")
}
// 5 fallthrough Use Just see this keyword , Unconditionally execute the next case
name:="lxx"
switch name {
case "lxx":
fmt.Println("lxx")
fallthrough
case "tom":
fmt.Println("tome")
fallthrough
default:
fmt.Println(" I don't know who it is ")
}
matters needing attention :
- case Duplicate options are not allowed after , If repeated, an error will be reported
边栏推荐
- Cloud service selection of enterprises: comparative analysis of SaaS, PAAS and IAAs
- "No war on the Western Front" we just began to love life, but we had to shoot at everything
- Haute performance et faible puissance Cortex - A53 Core Board | i.mx8m mini
- The 8th Blue Bridge Cup single chip microcomputer provincial competition
- JVM knowledge points
- Recently, the weather has been extremely hot, so collect the weather data of Beijing, Shanghai, Guangzhou and Shenzhen last year, and make a visual map
- Hands on deep learning (II) -- multi layer perceptron
- 蓝桥杯单片机省赛第五届
- 【leetcode】81. Search rotation sort array II
- go 函数
猜你喜欢
蓝桥杯单片机省赛第九届
蓝桥杯单片机第四届省赛
[wireless image transmission] FPGA based simple wireless image transmission system Verilog development, matlab assisted verification
[untitled]
【DesignMode】建造者模式(Builder model)
Nacos 配置中心整体设计原理分析(持久化,集群,信息同步)
Fourier series
Get started with Aurora 8b/10b IP core in one day (5) -- learn from the official routine of framing interface
Oracle的md5
Wpviewpdf Delphi and Net PDF viewing component
随机推荐
初识string+简单用法(二)
MySQL之账号管理
XSS prevention
UI (New ui:: MainWindow) troubleshooting
[designmode] Prototype Pattern
《西线无战事》我们才刚开始热爱生活,却不得不对一切开炮
How to solve the code error when storing array data into the database
5G時代全面到來,淺談移動通信的前世今生
Lost a few hairs, and finally learned - graph traversal -dfs and BFS
Homework in Chapter 3 of slam course of dark blue vision -- derivative application of T6 common functions
滴滴开源DELTA:AI开发者可轻松训练自然语言模型
蓝桥杯单片机省赛第十一届第二场
MD5 of Oracle
Blue Bridge Cup single chip microcomputer sixth temperature recorder
【IBDFE】基于IBDFE的频域均衡matlab仿真
Class design basis and advanced
Kotlin basic learning 14
微信小程序中 在xwml 中使用外部引入的 js进行判断计算
5G时代全面到来,浅谈移动通信的前世今生
手撕——排序