当前位置:网站首页>【Yugong Series】July 2022 Go Teaching Course 019-For Circular Structure
【Yugong Series】July 2022 Go Teaching Course 019-For Circular Structure
2022-07-31 00:22:00 【HUAWEI CLOUD】
一、循环结构
1.什么是循环
循环结构是指在程序中需要反复执行某个功能而设置的一种程序结构.它由循环体中的条件,判断继续执行某个功能还是退出循环.根据判断条件,循环结构又可细分为以下两种形式:先判断后执行的循环结构和先执行后判断的循环结构.
2.Go中的循环
在 Go 语言中循环的结构体格式如下:
for 初始语句;条件表达式;结束语句 { 循环体代码}for 循环会不停地进行循环,直到条件表达式返回 false 才推出循环,即执行 for 循环 “}” 后面的代码.
3.循环的基本使用
package mainimport "fmt"func main() { for i := 0; i < 100; i++ { fmt.Println("100个愚公") }}
初始语句还可以忽略不写,但是初始语句后面的分号必须写,代码如下:
package mainimport "fmt"func main() { i := 0 for ; i < 100; i++ { fmt.Println("100个愚公") }}上面的代码中将 i 放在了 for 循环前面初始化,这时 i 的作用域不止作用在 for 循环内.
4.死循环的使用
sum := 0for { sum++ if sum > 100 { break }}
5.案例
package mainimport "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 mainimport "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 mainimport "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 mainimport "fmt"func main() { for i := 0; i < 10; i++ { // 当 i 等于 2 时,执行 continue 语句,继续下一次循环 if i == 2 { continue } fmt.Printf("i: %d\n", i) }}
2.开始标签代码循环
package mainimport "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 } } }}
边栏推荐
- GO GOPROXY代理设置
- xss靶机训练【实现弹窗即成功】
- [In-depth and easy-to-follow FPGA learning 13---------Test case design 1]
- Steven Giesel recently published a 5-part series documenting his first experience building an application with the Uno Platform.
- MySQL grant statements
- Common network status codes
- 46.
- joiplay模拟器如何使用
- Homework: iptables prevent nmap scan and binlog
- Machine Learning 1-Regression Model (2)
猜你喜欢
随机推荐
2D Transform Module && Media Queries
论文理解:“Designing and training of a dual CNN for image denoising“
A Brief Talk About MPI
WMware Tools安装失败segmentation fault解决方法
正则表达式密码策略与正则回溯机制绕过
Game mall table establishment
[Meng Xin problem solving] Delete the Nth node from the bottom of the linked list
GO GOPROXY代理设置
Point Cloud Scene Reconstruction with Depth Estimation
什么是Promise?Promise的原理是什么?Promise怎么用?
How to import game archives in joiplay emulator
joiplay模拟器rtp如何安装
xss的绕过
firewalld
transition transition && animation animation
web漏洞之需要准备的工作
怎么开通代付通道接口?
xss靶机训练【实现弹窗即成功】
Word文件损坏如何修复
Ukraine's foreign ministry: wu was restored to complete the export of food security







![[Tang Yudi Deep Learning-3D Point Cloud Combat Series] Study Notes](/img/52/88ad349eca136048acd0f328d4f33c.png)
