当前位置:网站首页>[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
}
}
}
}

边栏推荐
- Unittest unit test framework full stack knowledge
- Behind every piece of information you collect, you can't live without TA
- Data output - image annotation and annotation
- 如何评估研发人员效能?软件工程师报告帮你看见每个人的贡献
- Uniapp summary (applet)
- sftp文件/文件夹上传服务器
- 执行 Add-Migration 迁移时报 Build failed.
- 你所不知道的WMS
- synchronized详解
- [深入研究4G/5G/6G专题-42]: URLLC-14-《3GPP URLLC相关协议、规范、技术原理深度解读》-8-低延时技术-2-基于slot的调度与Slot内灵活的上下行符号配比
猜你喜欢

Starfish Os X MetaBell战略合作,元宇宙商业生态更进一步

Go learning 01

SkyWalking分布式系统应用程序性能监控工具-中

Flex布局学习完成PC端

How to put app on the app store?

Cloud native enthusiast weekly: the evolution of Prometheus architecture

They are all talking about Devops. Do you really understand it?

Skywalking distributed system application performance monitoring tool - medium

借助Elephant&nbsp;Swap打造的ePLATO,背后的高溢价解析

Codeworks round 807 (Div. 2) a-c problem solution
随机推荐
Synchronized details
[Star Project] small hat aircraft War (VI)
QGIS mapping: vector data mapping process and export
每条你收藏的资讯背后,都离不开TA
华为APP UI自动化测试岗面试真题,真实面试经历。
Software testing interview question: what types of software testing are you familiar with?
Common problem types and methods of mathematical univariate differential proof problems in postgraduate entrance examination
[机缘参悟-53]:阳谋立身,阴谋防身
结构伪类选择器—查找单个—查找多个—nth-of-type和伪元素
54:第五章:开发admin管理服务:7:人脸入库流程;人脸登录流程;浏览器开启视频调试模式(以便能够在本机的不安全域名的情况下,也能去开启摄像头);
数字赋能 创新未来:海丝埃睿迪亮相第五届数字中国建设峰会
Starfish Os X MetaBell战略合作,元宇宙商业生态更进一步
【Star项目】小帽飞机大战(五)
视频常用分辨率
Vxe table/grid cell grouping and merging
Promise from introduction to mastery (Chapter 2 understanding and use of promise)
11 Django basics database operation
探究flex-basis
埃睿迪再度亮相数字中国峰会 持续深化用科技守护绿水青山
The level "trap" of test / development programmers is not a measure of one-dimensional ability