当前位置:网站首页>Introduction to go language (VI) -- loop statement
Introduction to go language (VI) -- loop statement
2022-06-11 19:10:00 【AI Xing】
Three forms of loop statements
And C、java、python The difference is GO There is only for Loop statement , however Go Of for Circular statements can also be used in other languages while The function of sentences
Here are three for The use of recycling
routine for loop
for i:= Initial value ; i <= Termination value ; i++ {
// Code block
}
act as while Of for loop
i:= Initial value
for i < End value {
// Code block
// i Change towards the end value
}
for And range In combination with
It should be noted that for And range When iterating with, the iteration variables must be of iteration type : Array or dictionary
for idx, value range m {
// m Is an iteratable variable
// Code block
}
for Loop example
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)
}
}

End cycle
break
If you write directly in the loop break be break Quit yes break In the cycle ; meanwhile Go Allows us to define a tag before entering a layer loop , Can be in break When you specify a label, you can exit the cycle specified by the label directly
Example
Let's assume that we have two cycles , We are going to find the sum of two cycles 10 To exit the loop
direct break The situation of
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
}
}
}
}

According to the running results, it does not meet our requirements , Because it's used alone break You can only exit the current break In the cycle
Tagged 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 The basic definition of is no different from other languages , And break equally , It can also define a tag , direct continue Loop under label
边栏推荐
- collect.stream().collect()方法的使用
- kubernetes 二进制安装(v1.20.15)(八)部署 网络插件
- MySQL in-depth and complete learning - stage 1 - overview of learning
- Deploy a go MSSQL API endpoint on SAP kyma
- 5g communication test manual based on Ti am5728 + artix-7 FPGA development board (dsp+arm)
- Construct enemy tanks
- Practice of tag recognition based on Huawei cloud image
- Use Mysql to determine the day of the week
- mysql 联合索引和BTree
- 构造敌方坦克
猜你喜欢

【视频去噪】基于SALT实现视频去噪附Matlab代码

Cool visualization tool: first introduction to processing
![[untitled]](/img/02/49d333ba80bc6a3e699047c0c07632.png)
[untitled]

cf:D. Black and White Stripe【连续k个中最少的个数 + 滑动窗口】

Dynamic explosion effect

uni-app 慕客热搜项目实战(一)tabBar的制作
![Leetcode: sword finger offer 56 - ii Number of occurrences of numbers in the array II [simple sort]](/img/cf/7879b320720e1b6f98f1a8bd2feae2.png)
Leetcode: sword finger offer 56 - ii Number of occurrences of numbers in the array II [simple sort]

Gmail: how do I recall an outgoing message?

程序员10年巨变,一切都变了又好像没变...

一款自适应的聊天网站-匿名在线聊天室PHP源码
随机推荐
cf:B. Array Decrements【模拟】
Today's sleep quality record is 60 points
[image denoising] image denoising based on Markov random field with matlab code
Database design graduation information management
Deploy a go MSSQL API endpoint on SAP kyma
使用贝叶斯优化进行深度神经网络超参数优化
mysql 联合索引和BTree
防止敌方坦克重叠
使用canvas给页面添加文字水印
我不太想在网上开户,网上股票开户安全吗?
KMP!你值得拥有!!! 直接运行直接跑!
[Multisim Simulation] generate square wave and triangular wave generators by operational amplifier
实现可以继续上局
MOS transistor 24n50 parameters of asemi, 24n50 package, 24n50 size
01. Telecommunications_ Field business experience
Undefined reference to 'g2o:: vertexe3:: vertexe3()'
kubernetes 二进制安装(v1.20.15)(八)部署 网络插件
[signal denoising] signal denoising based on FFT and fir with matlab code
美国通胀率8.6%创41年历史新高!通胀高烧不退?股票、加密市场先跌为敬!
Replace the backbone of target detection (take the fast RCNN as an example)