当前位置:网站首页>[Yugong series] July 2022 go teaching course 019 - for circular structure
[Yugong series] July 2022 go teaching course 019 - for circular structure
2022-07-28 02:17:00 【Yugong move code】
List of articles
One 、 Loop structure
1. What is cycle
Loop structure refers to a program structure that needs to perform a function repeatedly in a program . It is by the conditions in the circulatory body , Determine whether to continue a function or exit the loop . According to the judgment condition , The circular structure can be subdivided into the following two forms : The loop structure of judgment before execution and the loop structure of execution before judgment .
2.Go The cycle in
stay Go The structure format of the loop in the language is as follows :
for The initial statement ; Conditional expression ; The end of the statement {
Loop body code
}
for The cycle will go on and on , Until the conditional expression returns false Just launch the cycle , The perform for loop “}” Later code .
3. Basic use of recycling
package main
import "fmt"
func main() {
for i := 0; i < 100; i++ {
fmt.Println("100 A foolish man ")
}
}

The initial statement can also be ignored without writing , But the semicolon after the initial statement must be written , The code is as follows :
package main
import "fmt"
func main() {
i := 0
for ; i < 100; i++ {
fmt.Println("100 A foolish man ")
}
}
The code above will i On the for Initialize before the loop , At this time i The scope of is not limited to for Within the loop .
4. The use of dead circulation
sum := 0
for {
sum++
if sum > 100 {
break
}
}

5. Case study
package main
import "fmt"
func main() {
// Traverse , Decide what line to deal with
for y := 1; y <= 9; y++ {
// Traverse , Decide how many columns there are in this row
for x := 1; x <= y; x++ {
fmt.Printf("%d*%d=%d ", x, y, x*y)
}
// Manually generate carriage returns
fmt.Println()
}
}

Two 、break sentence
Go In language break Statements can end for、switch and select Code block for , in addition break Statements can also be tagged after statements , Indicates to exit the code block corresponding to a tag , Label requirements must be defined in the corresponding for、switch and select On the code block of .
1. Out of the loop
package main
import "fmt"
func main() {
// Outer loop
for i := 0; i < 10; i++ {
fmt.Printf("i: %d\n", i)
// When i be equal to 6 when , Jump to loop
if i == 6 {
// Out of the loop
break
}
}
fmt.Println(" Out of the loop ...")
}

2. Jump out of label code block
package main
import "fmt"
func main() {
OuterLoop:
for i := 0; i < 2; i++ {
for j := 0; j < 5; j++ {
switch j {
case 2:
fmt.Println(i, j)
break OuterLoop
case 3:
fmt.Println(i, j)
break OuterLoop
}
}
}
}

3、 ... and 、continue sentence
Go In language continue Statement can end the current loop , Start the next iteration of the loop , Only in for Use in cycle , stay continue When you add a label after a statement , Represents the loop corresponding to the start tag .
1. Start the next cycle
package main
import "fmt"
func main() {
for i := 0; i < 10; i++ {
// When i be equal to 2 when , perform continue sentence , So let's go to the next loop
if i == 2 {
continue
}
fmt.Printf("i: %d\n", i)
}
}

2. Start label code cycle
package main
import "fmt"
func main() {
OuterLoop:
for i := 0; i < 2; i++ {
for j := 0; j < 5; j++ {
switch j {
case 2:
fmt.Println(i, j)
continue OuterLoop
}
}
}
}

边栏推荐
- Which database is the fastest to query data only?
- [database data recovery] data recovery case of insufficient disk space of SQL Server database
- MySQL高可用和主从同步
- The cooperation between starfish OS and metabell is just the beginning
- Leetcode hot topic Hot 100 - > 1. Sum of two numbers
- Software testing interview question: what is the purpose of test planning? What are the contents of the test plan? Which are the most important?
- Flume (5 demos easy to get started)
- Product interpretation - Design and distributed expansion of metersphere UI test module
- 小米网站主页面大模块——小模块+导航(浮动案例)
- Flume(5个demo轻松入门)
猜你喜欢

How to evaluate the effectiveness of R & D personnel? Software Engineer reports help you see everyone's contribution

Under the new retail format, retail e-commerce RPA helps reshape growth

Vxe Table/Grid 单元格分组合并

Promise从入门到精通 (第2章 Promise的理解和使用)

这个操作可能不值钱,但却值得学习 | 【图片批量裁剪】

Codeworks round 810 (Div. 2) a~c problem solution

智能合约安全——selfdestruct攻击

Codeforces Round #807 (Div. 2) A-C题解
![[database data recovery] data recovery case of insufficient disk space of SQL Server database](/img/0e/908db40e1e8b7dd62e12558c1c6dc4.png)
[database data recovery] data recovery case of insufficient disk space of SQL Server database

54:第五章:开发admin管理服务:7:人脸入库流程;人脸登录流程;浏览器开启视频调试模式(以便能够在本机的不安全域名的情况下,也能去开启摄像头);
随机推荐
Which database is the fastest to query data only?
样本不均衡-入门0
IT这个岗位,人才缺口百万,薪资水涨船高,上不封顶
Appium 点击操作梳理
Software testing interview question: what types of software testing are you familiar with?
Huawei app UI automation test post interview real questions, real interview experience.
WMS you don't know
Flex开发网页实例web端
mongodb/mongoTemplate.upsert批量插入更新数据的实现
视频常用分辨率
Go learn 02 basic knowledge
Day6 函数和模块的使用
Four common post data submission methods
Understand the "next big trend" in the encryption industry - ventures Dao
智能合约安全——selfdestruct攻击
[深入研究4G/5G/6G专题-42]: URLLC-14-《3GPP URLLC相关协议、规范、技术原理深度解读》-8-低延时技术-2-基于slot的调度与Slot内灵活的上下行符号配比
54:第五章:开发admin管理服务:7:人脸入库流程;人脸登录流程;浏览器开启视频调试模式(以便能够在本机的不安全域名的情况下,也能去开启摄像头);
Flex layout learning completed on PC side
Behind every piece of information you collect, you can't live without TA
新零售业态下,零售电商RPA助力重塑增长