当前位置:网站首页>Go语言入门(六)——循环语句
Go语言入门(六)——循环语句
2022-06-11 19:06:00 【艾醒】
循环语句的三种形式
与C、java、python不同的是GO只存在for循环语句,但是Go的for循环语句也能够起到其他语言中while语句的作用
下面展示三种for循环的使用方式
常规for循环
for i:=初始值; i <= 终止值; i++ {
//代码块
}
充当while的for循环
i:=初始值
for i < 结束值 {
// 代码块
// i向着结束值方向改变
}
for与range配合使用
需要注意的是for与range配合进行迭代时迭代的变量必须可迭代类型:数组或者字典
for idx, value range m {
// m为可迭代的变量
//代码块
}
for 循环示例
package main
import "fmt"
func main(){
for i:=0; i < 10; i++ {
fmt.Printf("i:%v\n", i)
}
fmt.Printf("\n")
i:=0
for i < 10 {
fmt.Printf("i:%v\n", i)
i++
}
var a = []int{
1, 2, 3, 4, 5}
for idx, value := range a {
fmt.Printf("idx:%v, value:%v\n", idx, value)
}
mp:=make(map[int]string, 0)
mp[0] = "hello"
mp[1] = "world"
for key, value := range mp {
fmt.Printf("key:%v, value:%v\n", key, value)
}
}

终止循环
break
如果在循环中直接写break则break退出的是break所在的循环;同时Go允许我们在进入某一层循环前定义一个标签,可以在break时指定标签使得可以直接退出标签指定的循环
示例
我们假定我们有两个循环,我们要在找到两循环和为10的情况下退出循环
直接break的情况
package main
import "fmt"
func main(){
for i:=0; i < 10; i++ {
for j:=0; j < 10; j++ {
fmt.Printf("i:%v, j:%v, sum:%v\n", i, j, i + j)
if i + j >= 10{
break
}
}
}
}

从运行结果来看并没有达到我们的要求,因为单独使用break只能退出当前break所在的循环
带标签的break
package main
import "fmt"
func main(){
ENDLABEL:
for i:=0; i < 10; i++ {
for j:=0; j < 10; j++ {
fmt.Printf("i:%v, j:%v, sum:%v\n", i, j, i + j)
if i + j >= 10{
break ENDLABEL
}
}
}
}

continue
continue的基本定义和其他语言无异,与break一样,它也可以定义一个标签,直接continue标签下的循环
边栏推荐
- ASEMI的MOS管25N120在不同应用场景的表现
- Given a project, how will you conduct performance testing?
- 《经济学人》:WTO MC12重启 数字经济成为全球经济复苏和增长的核心引擎
- 今天睡眠质量记录60分
- Web3游戏:游戏体验的探寻与重塑
- I don't want to open an account online. Is it safe to open an account online?
- 使用图像处理技术和卷积神经网络(CNN)的作物病害检测
- Flink CDC 在大健云仓的实践
- 在 SAP Kyma 上部署一个 Go MSSQL API Endpoint
- 给你一个项目,你将如何开展性能测试工作?
猜你喜欢

2023年西安交通大学管理学院MPAcc提前批面试网报通知

使用图像处理技术和卷积神经网络(CNN)的作物病害检测
開發中必備的文件的上傳與下載
![Leetcode: sword finger offer 59 - ii Maximum value of queue [deque + sortedlist]](/img/6b/f2e04cd1f3aaa9fe057c292301894a.png)
Leetcode: sword finger offer 59 - ii Maximum value of queue [deque + sortedlist]

【Multisim仿真】利用运算放大器产生锯齿波

Key contents that wwdc22 developers need to pay attention to

Swagger2 easy to use

视觉SLAM十四讲笔记-10-1

全国院校MBA、EMBA、MPA、MEM、提前面试(预面试)时间批次已出(持续更新中)-文都管联院

Specific methods for JS to realize full screen display
随机推荐
Web3 Games: exploring and reshaping the game experience
Flink CDC 在大健云仓的实践
Internet_ Business Analysis Overview
Dynamic explosion effect
Overall process of software development
MBA, EMBA, MPa, MEM, pre interview (pre interview) time batch of national colleges and universities has been released (continuously updated) - Wendu Management Institute
Let our tanks move happily
leetcode:66. add one-tenth
leetcode:926. 将字符串翻转到单调递增【前缀和 + 模拟分析】
Financial bank_ Introduction to collection system
Function development of user information management
Deploy a go MSSQL API endpoint on SAP kyma
手把手教你学会FIRST集和FOLLOW集!!!!吐血收藏!!保姆级讲解!!!
[image segmentation] image segmentation based on Markov random field with matlab code
Use canvas to add text watermark to the page
Specific methods for JS to realize full screen display
cf:G. Count the Trains【sortedset + bisect + 模拟维持严格递减序列】
[signal denoising] signal denoising based on FFT and fir with matlab code
关于富文本储存数据库格式转译问题
The US inflation rate reached a 41 year high of 8.6%! High inflation fever? The stock and encryption markets fell first!