当前位置:网站首页>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
边栏推荐
- Basic syntax of unity script (8) - collaborative program and destruction method
- [Li Kou brush questions] 15 Sum of three numbers (double pointer); 17. Letter combination of phone number (recursive backtracking)
- go 函数
- 高性能 低功耗Cortex-A53核心板 | i.MX8M Mini
- Hands on deep learning (II) -- multi layer perceptron
- Fourier series
- 傅里叶级数
- 【leetcode】81. Search rotation sort array II
- Raspberry pie GPIO pin controls traffic light and buzzer
- The second game of the 11th provincial single chip microcomputer competition of the Blue Bridge Cup
猜你喜欢
![[live broadcast review] the first 8 live broadcasts of battle code Pioneer have come to a perfect end. Please look forward to the next one!](/img/46/d36ae47c3d44565d695e8ca7f34980.jpg)
[live broadcast review] the first 8 live broadcasts of battle code Pioneer have come to a perfect end. Please look forward to the next one!

蓝桥杯单片机省赛第十二届第二场

Failed to upgrade schema, error: “file does not exist

Wpviewpdf Delphi and Net PDF viewing component

高性能 低功耗Cortex-A53核心板 | i.MX8M Mini

集成底座方案演示说明

JVM知识点

Interface debugging tool simulates post upload file - apipost
![[personal notes] PHP common functions - custom functions](/img/3d/d50622e3ddb08f654f30063e8226ac.jpg)
[personal notes] PHP common functions - custom functions

C language: examples of logical operation and judgment selection structure
随机推荐
go 变量与常量
go 语言命名规范
Basic syntax of unity script (8) - collaborative program and destruction method
SQL Yiwen get window function
Welcome the winter vacation multi school league game 2 partial solution (B, C, D, F, G, H)
Visual slam Lecture 3 -- Lie groups and Lie Algebras
Wpviewpdf Delphi and Net PDF viewing component
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
Homework in Chapter 3 of slam course of dark blue vision -- derivative application of T6 common functions
C language: examples of logical operation and judgment selection structure
Vite: configure IP access
Xlwings drawing
蓝桥杯单片机省赛第十届
【直播回顾】战码先锋首期8节直播完美落幕,下期敬请期待!
软件测试人的第一个实战项目:web端(视频教程+文档+用例库)
Oracle viewing locked tables and unlocking
The 5th Blue Bridge Cup single chip microcomputer provincial competition
2022-07-01:某公司年会上,大家要玩一食发奖金游戏,一共有n个员工, 每个员工都有建设积分和捣乱积分, 他们需要排成一队,在队伍最前面的一定是老板,老板也有建设积分和捣乱积分, 排好队后,所有
Monkey test
5G時代全面到來,淺談移動通信的前世今生