当前位置:网站首页>【愚公系列】2022年07月 Go教学课程 019-循环结构之for
【愚公系列】2022年07月 Go教学课程 019-循环结构之for
2022-07-28 00:48:00 【愚公搬代码】
文章目录
一、循环结构
1.什么是循环
循环结构是指在程序中需要反复执行某个功能而设置的一种程序结构。它由循环体中的条件,判断继续执行某个功能还是退出循环。根据判断条件,循环结构又可细分为以下两种形式:先判断后执行的循环结构和先执行后判断的循环结构。
2.Go中的循环
在 Go 语言中循环的结构体格式如下:
for 初始语句;条件表达式;结束语句 {
循环体代码
}
for 循环会不停地进行循环,直到条件表达式返回 false 才推出循环,即执行 for 循环 “}” 后面的代码。
3.循环的基本使用
package main
import "fmt"
func main() {
for i := 0; i < 100; i++ {
fmt.Println("100个愚公")
}
}

初始语句还可以忽略不写,但是初始语句后面的分号必须写,代码如下:
package main
import "fmt"
func main() {
i := 0
for ; i < 100; i++ {
fmt.Println("100个愚公")
}
}
上面的代码中将 i 放在了 for 循环前面初始化,这时 i 的作用域不止作用在 for 循环内。
4.死循环的使用
sum := 0
for {
sum++
if sum > 100 {
break
}
}

5.案例
package main
import "fmt"
func main() {
// 遍历, 决定处理第几行
for y := 1; y <= 9; y++ {
// 遍历, 决定这一行有多少列
for x := 1; x <= y; x++ {
fmt.Printf("%d*%d=%d ", x, y, x*y)
}
// 手动生成回车
fmt.Println()
}
}

二、break语句
Go语言中 break 语句可以结束 for、switch 和 select 的代码块,另外 break 语句还可以在语句后面添加标签,表示退出某个标签对应的代码块,标签要求必须定义在对应的 for、switch 和 select 的代码块上。
1.跳出循环
package main
import "fmt"
func main() {
// 外循环
for i := 0; i < 10; i++ {
fmt.Printf("i: %d\n", i)
// 当 i 等于 6 时,跳转到循环
if i == 6 {
// 跳出循环
break
}
}
fmt.Println("跳出循环 ...")
}

2.跳出标签代码块
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
}
}
}
}

三、continue语句
Go语言中 continue 语句可以结束当前循环,开始下一次的循环迭代过程,仅限在 for 循环内使用,在 continue 语句后添加标签时,表示开始标签对应的循环。
1.开始下一次循环
package main
import "fmt"
func main() {
for i := 0; i < 10; i++ {
// 当 i 等于 2 时,执行 continue 语句,继续下一次循环
if i == 2 {
continue
}
fmt.Printf("i: %d\n", i)
}
}

2.开始标签代码循环
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
}
}
}
}

边栏推荐
- Fluorite network, difficult to be a "lone brave"
- C# 使用Abp仓储访问数据库时报错记录集
- Flex布局—固定定位+流式布局—主轴对齐—侧轴对齐—伸缩比
- Interviewer: are you sure redis is a single threaded process?
- A letter to the user of qubu drawing bed
- [Taichi] draw a regular grid in Tai Chi
- Five basic data structures of redis
- Software test interview questions: common post data submission methods
- The level "trap" of test / development programmers is not a measure of one-dimensional ability
- 数据输出-绘制动图
猜你喜欢

Flex开发网页实例web端

每条你收藏的资讯背后,都离不开TA

萤石网络,难当「孤勇者」

Interviewer: are you sure redis is a single threaded process?

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

54: Chapter 5: develop admin management services: 7: face warehousing process; Face login process; The browser turns on the video debugging mode (so that the camera can also be turned on in the case o

Flex development web page instance web side

Codeforces Round #807 (Div. 2) A-C题解

ArcGIS: loading historical remote sensing images

Codeworks round 810 (Div. 2) a~c problem solution
随机推荐
软件测试面试题:你认为做好测试用例设计工作的关键是什么?
Promise从入门到精通 (第2章 Promise的理解和使用)
Data security and privacy computing summit - provable security: Learning
Software testing interview question: what types of software testing are you familiar with?
Leveraging the blue ocean of household appliances consumption with "digital channels", the dealer online system enables enterprises to further their business
Gbase 8C transaction ID and snapshot (VI)
Software testing interview question: what is the purpose of test planning? What are the contents of the test plan? Which are the most important?
Software testing interview question: what do you think is the key to good test case design?
Gbase 8C transaction ID and snapshot
二叉树的遍历和性质
Gbase 8C recovery control function
Unity universal red dot system
ArcGIS:加载历史遥感影像
A lock faster than read-write lock. Don't get to know it quickly
BGP federal experiment
你所不知道的WMS
Flume(5个demo轻松入门)
【网站搭建】使用acme.sh更新ssl证书:将zerossl改为letsencrypt
一种比读写锁更快的锁,还不赶紧认识一下
Common modules of ros2 launch files