当前位置:网站首页>(7) Loop statement for
(7) Loop statement for
2022-06-12 17:08:00 【Binary cup don't stop】
( 7、 ... and ) Loop statement for
1 for keyword
1.1 Standard writing
and C Linguistic for equally :
for init; condition; post {
}
- 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 .
example : Calculation 1 To 10 The sum of the numbers :
package main
import "fmt"
func main() {
sum := 0
for i := 0; i <= 10; i++ {
sum += i
}
fmt.Println(sum)
}
1.2 And C Language while similar
and C Of while equally :
for condition {
}
example : stay sum Less than 10 When the sum The value after self adding
package main
import "fmt"
func main() {
sum := 1
// It's OK to write like this , More like While Statements in the form of
for sum <= 10{
sum ++
}
fmt.Println(sum)
}
1.3 Infinite loop
and C Of for(; equally :
for {
}
example : Infinite loop
package main
import "fmt"
func main() {
sum := 0
for {
sum++ // Go on indefinitely
}
fmt.Println(sum) // Unable to output
}
1.4 Statements that jump out of the loop
- break: Jump out of the loop .
- continue: Jump out of a cycle .
1.5 Composite types use for-each range loop
For-each range loop
The loop in this format can be used for string 、 Array 、 Slice and so on to iterate the output elements .
package main
import "fmt"
func main() {
strings := []string{
"google", "runoob"}
for i, s := range strings {
fmt.Println(i, s)
}
numbers := [6]int{
1, 2, 3, 5}
for i,x:= range numbers {
fmt.Printf(" The first %d position x Value = %d\n", i,x)
}
}
The output of the above example is ::
0 google
1 runoob
The first 0 position x Value = 1
The first 1 position x Value = 2
The first 2 position x Value = 3
The first 3 position x Value = 5
The first 4 position x Value = 0
The first 5 position x Value = 0
for Cyclic range The format can be omitted key and value, The following example :
package main
import "fmt"
func main() {
map1 := make(map[int]float32)
map1[1] = 1.0
map1[2] = 2.0
map1[3] = 3.0
map1[4] = 4.0
// Read key and value
for key, value := range map1 {
fmt.Printf("key is: %d - value is: %f\n", key, value)
}
// Read key
for key := range map1 {
fmt.Printf("key is: %d\n", key)
}
// Read value
for _, value := range map1 {
fmt.Printf("value is: %f\n", value)
}
}
The output of the above example is :
key is: 4 - value is: 4.000000
key is: 1 - value is: 1.000000
key is: 2 - value is: 2.000000
key is: 3 - value is: 3.000000
key is: 1
key is: 2
key is: 3
key is: 4
value is: 1.000000
value is: 2.000000
value is: 3.000000
value is: 4.000000
边栏推荐
- 邱盛昌:OPPO商业化数据体系建设实战
- Gerrit+2触发Jenkins任务
- 布局管理中的sizePolicy的策略问题
- Information outline recording tool: omnioutliner 5 Pro Chinese version
- How to play the map with key as assertion
- R语言使用epiDisplay包的aggregate.plot函数可视化每个子集的汇总统计信息(可视化基于单个分组下的阳性指标的概率值及其95%置信区间、基于折线图、仅仅适用于目标类别为二分类)
- \Begin{algorithm} notes
- 薛定谔的日语学习小程序源码
- Kill program errors in the cradle with spotbugs
- Is the securities account opened by qiniu safe? Is it legal?
猜你喜欢

Sizepolicy policy in layout management

Some minor problems and solutions encountered when using ubantu

叶子分享站PHP源码下载

Gerrit triggers Jenkins sonarqube scan

使用ubantu时,遇见的一些小毛病和解决方法

Cicada mother talks to rainbow couple: 1.3 billion goods a year, from e-commerce beginners to super goods anchor

Download PHP source code of leaf sharing station

Installation and use of rolabelimg

Recommend AI intelligent drawing repair software

两位新晋Committer的“升级攻略”
随机推荐
1723. 完成所有工作的最短时间
云开发坤坤鸡乐盒微信小程序源码
goland变成中文版了怎么修改回英文版
Male god goddess voting source code v5.5.21 voting source code
JS using RSA encryption and decryption
Pat class a 1142 largest regiment
Sudo of uabntu
添加静态路由
R语言使用plot函数可视化数据散点图,使用font.axis参数指定坐标轴刻度标签的字体类型为斜体字体(italic)
Structural requirement analysis of software engineering student information management system
博士申請 | 新加坡國立大學Xinchao Wang老師招收圖神經網絡方向博士/博後
Gerrit triggers Jenkins sonarqube scan
布局管理中的sizePolicy的策略问题
[adult Liu Er - pytorch deep learning practice] notes with learning (I)
Swin Transformer代码讲解
并发三色标记法
Recommend 6 open source projects of yyds
5-5配置Mysql复制 基于日志点的复制
大端模式和小端模式的区别
The R language uses the tabpct function of the epidisplay package to generate a two-dimensional contingency table, and uses the mosaic diagram to visualize the contingency table (two-dimensional conti