当前位置:网站首页>For loop of go language foundation
For loop of go language foundation
2022-06-23 07:52:00 【Ink purple feather ink】
One 、 Loop statement
Loop statements are used to repeatedly execute a piece of code .
for yes Go The only loop statement in language .
Go Linguistic For Circulatory 3 In the form of , Only one of them uses semicolons .
for init; condition; post { }
for condition { }
for { }init: It's usually an assignment expression , Assign initial values to control variables ; condition: A relational or logical expression , Cycle control conditions ; post: It's usually an assignment expression , Increment or decrement of a control variable . for The execution of the statement is as follows :
- Let's start with the expression
initAssign initial value to ; - Discriminating assignment expression init Whether the given
conditionConditions , If the value is true , Satisfy the cyclic condition , Then execute the statement inside the loop , And then executepost, Enter the second loop , Judge againcondition; Otherwise, judgmentconditionThe value of is false , Not meeting the conditions , On termination for loop , Execute the out of loop statement .
and if Conditional statements are the same , Loop statements can also be nested
for [condition | ( init; condition; increment ) | Range]
{
for [condition | ( init; condition; increment ) | Range]
{
statement(s)
}
statement(s)
}Two 、 Cycle control
breakStatement is used to terminate abruptly before completing normal execution for loop , Then the program will be in for The next line of the loop starts executing .continueStatements are used to jump out offorThe current loop in the loop . staycontinueAll after the statementforLoop statements are not executed in this loop . The loop will continue to execute in the next loop .
3、 ... and 、 Example
package main
import "fmt"
func test1() {
// normal for loop
for i := 0; i < 3; i++ {
fmt.Printf("i=%d\t", i)
}
}
func test2() {
// Anamorphic writing
i := 0
for i < 3 {
fmt.Printf("i=%d\t", i)
i = i + 1
}
}
func main() {
test1()
fmt.Println()
test2()
}Four 、 Infinite loop
func test3() {
for true {
fmt.Println("Hello World")
}
}
// Or just put true Also omitted
func test3() {
for {
fmt.Println("Hello World")
}
} tip : Infinite loop careful use ,Ctrl+c You can stop the examples in this article .
边栏推荐
- MySQL系统表介绍
- RFID data security experiment: C # visual realization of parity check, CRC redundancy check and Hamming code check
- 1. probability theory - combination analysis
- 在线JSON转CSharp(C#)Class工具
- 1.概率论-组合分析
- 带你玩tiktok就这么简单
- Can you think of a better way to solve the problem of string inversion?
- How do I install MySQL on my computer?
- 3dmax插件开发环境配置及FileExport和Utilities模板测试
- Guava Cache 使用小结
猜你喜欢
![[veusz] import 2D data in CSV](/img/22/467139f5a83ce9e88a57ced732d4d6.png)
[veusz] import 2D data in CSV

通过端口查文件

Acwing第 56 场周赛【完结】

Deploy kubersphere in kubernetes

QT reading XML files using qdomdocument

Matlab随机波动率SV、GARCH用MCMC马尔可夫链蒙特卡罗方法分析汇率时间序列

QT project error: -1: error: cannot run compiler 'clang++' Output:mingw32-make. exe

Eureka服务注册与发现

AVL树的实现

Qt工程报错:-1: error: Cannot run compiler ‘clang++‘. Output:mingw32-make.exe
随机推荐
【markdown】markdown 教程大归纳
Quick sort + bubble sort + insert sort + select sort
Unity picture loading and saving
左乘右乘矩阵问题
快速删除代码里面的node_modules
【星球精选】如何高效构建 Roam 与 theBrain 间细粒度双向链接?
数学知识:快速幂求逆元—快速幂
Query on the performance of multi table view in MySQL
[cloud computing event] vocational skill competition -- container development example pig rapid development framework
[深度学习][原创]如何不用yolov5权重或者模型进行目标检测和绘制map等参数图
EXCEL VBA 入门与实用例子
分布式ID生成
Interview questions of a company in a certain month of a certain year (1)
Can you think of a better way to solve the problem of string inversion?
Introduction to MySQL system tables
Which company would like to buy serious illness insurance in 2022?
Apache Solr 任意文件读取复现
快速排序 + 冒泡排序 + 插入排序 + 選擇排序
Friends of the week
WPS for thesis writing installs MathType plug-in to write mathematical formulas