当前位置:网站首页>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
边栏推荐
- Financial bank_ Introduction to collection system
- [image segmentation] image segmentation based on Markov random field with matlab code
- mysql 联合索引和BTree
- MySQL in-depth and complete learning - stage 1 - overview of learning
- Cf:g. count the trains [sortedset + bisect + simulation maintaining strict decreasing sequence]
- PyMySQL利用游标操作数据库方法封装!!!
- The US inflation rate reached a 41 year high of 8.6%! High inflation fever? The stock and encryption markets fell first!
- Construct enemy tanks
- Use canvas to add text watermark to the page
- uni-app 慕客热搜项目实战(一)tabBar的制作
猜你喜欢

2022-2023 MEM pre approval interview notice of School of management, Xi'an Jiaotong University

Performance of MOS transistor 25n120 of asemi in different application scenarios

SQL injection vulnerability learning 1: phpstudy integrated environment building DVWA shooting range
![[image segmentation] image segmentation based on Markov random field with matlab code](/img/62/874b0ac3e1cbb7cad9c3a77da391d7.png)
[image segmentation] image segmentation based on Markov random field with matlab code
開發中必備的文件的上傳與下載

Cf:c. restoring the duration of tasks

Uni app Muke hot search project (I) production of tabbar

Go语言入门(六)——循环语句

On the translation of rich text storage database format

The US inflation rate reached a 41 year high of 8.6%! High inflation fever? The stock and encryption markets fell first!
随机推荐
Gmail: how do I recall an outgoing message?
MySQL federated index and BTREE
On the selection and design of management subsystem of collection system
使用贝叶斯优化进行深度神经网络超参数优化
[image denoising] image denoising based on Markov random field with matlab code
Make a static tank
MOS transistor 24n50 parameters of asemi, 24n50 package, 24n50 size
Introduction to ieda bottom menu
[video denoising] video denoising based on salt with matlab code
Project management of workflow and business service on SAP BTP
5g communication test manual based on Ti am5728 + artix-7 FPGA development board (dsp+arm)
I don't want to open an account online. Is it safe to open an account online?
Understand how to get started with machine learning to quantify transactions?
Visual slam lecture notes-10-1
美国通胀率8.6%创41年历史新高!通胀高烧不退?股票、加密市场先跌为敬!
On Workflow selection
Database design graduation information management
记录一下phpstudy配置php8.0和php8.1扩展redis
Kubernetes binary installation (v1.20.15) (VIII) deploying network plug-ins
[image segmentation] image segmentation based on Markov random field with matlab code