当前位置:网站首页>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
边栏推荐
- 蓝桥杯单片机省赛第十二届第一场
- 潘多拉 IOT 开发板学习(RT-Thread)—— 实验1 LED 闪烁实验(学习笔记)
- Opencv learning example code 3.2.4 LUT
- Vite: configure IP access
- Homework in Chapter 3 of slam course of dark blue vision -- derivative application of T6 common functions
- Which product of anti-cancer insurance is better?
- [yolo3d]: real time detection of end-to-end 3D point cloud input
- VS2010 plug-in nuget
- 【力扣刷题】15.三数之和(双指针);17.电话号码的字母组合(递归回溯)
- 软件测试人的第一个实战项目:web端(视频教程+文档+用例库)
猜你喜欢

Suggestions on settlement solution of u standard contract position explosion

手撕——排序

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

滴滴开源DELTA:AI开发者可轻松训练自然语言模型

The 5th Blue Bridge Cup single chip microcomputer provincial competition

Nacos 配置中心整体设计原理分析(持久化,集群,信息同步)

【leetcode】74. Search 2D matrix

潘多拉 IOT 开发板学习(RT-Thread)—— 实验1 LED 闪烁实验(学习笔记)

"No war on the Western Front" we just began to love life, but we had to shoot at everything

C language: examples of logical operation and judgment selection structure
随机推荐
【小技巧】使用matlab GUI以对话框模式读取文件
The first game of the 12th Blue Bridge Cup single chip microcomputer provincial competition
Raspberry pie GPIO pin controls traffic light and buzzer
C language: examples of logical operation and judgment selection structure
蓝桥杯单片机省赛第十一届第一场
5g era is coming in an all-round way, talking about the past and present life of mobile communication
毕设-基于SSM电影院购票系统
手撕——排序
The second game of the 12th provincial single chip microcomputer competition of the Blue Bridge Cup
Interface debugging tool simulates post upload file - apipost
Visual slam Lecture 3 -- Lie groups and Lie Algebras
Which is better, industrial intelligent gateway or edge computing gateway? How to choose the right one?
Hands on deep learning (II) -- multi layer perceptron
The first practical project of software tester: web side (video tutorial + document + use case library)
The fourth provincial competition of Bluebridge cup single chip microcomputer
高性能 低功耗Cortex-A53核心板 | i.MX8M Mini
The 5th Blue Bridge Cup single chip microcomputer provincial competition
[designmode] Prototype Pattern
Set vscode. When double clicking, the selected string includes the $symbol - convenient for PHP operation
Blue Bridge Cup SCM digital tube skills