当前位置:网站首页>(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
边栏推荐
- The R language uses the tablestack function of epidisplay package to generate statistical analysis tables based on grouped variables (including descriptive statistical analysis, hypothesis test, diffe
- How to play the map with key as assertion
- 使用ubantu时,遇见的一些小毛病和解决方法
- PAT甲级 1142 最大团
- Structural requirement analysis of software engineering student information management system
- PAT甲级 1139 第一次接触
- Concurrent trichromatic marking
- [adult Liu Er - pytorch deep learning practice] notes with learning (I)
- Qiushengchang: Practice of oppo commercial data system construction
- 5-5 configuring MySQL replication log point based replication
猜你喜欢

ShardingJDBC 分库分表详解

软件工程 学生信息管理系统 结构化的需求分析

Atlas conflict Remote Code Execution Vulnerability (cve-2022-26134) vulnerability recurrence

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

邱盛昌:OPPO商业化数据体系建设实战

Microsoft Office MSDT代码执行漏洞(CVE-2022-30190)漏洞复现

有趣的 LD_PRELOAD

Installation and use of rolabelimg

Cloud development kunkun chicken music box wechat applet source code

D. master router setting and 401 networking
随机推荐
Detailed explanation of shardingjdbc database and table
Go的变量
写技术博客的意义
PAT甲级 1139 第一次接触
5、Embedding
Unit sshd.service could not be found
C#期末复习编程题(老师猜的)
Making nearly $90billion, Buffett's latest heavy stock exposure
JVM memory model and local memory
Swintransformer network architecture
pytorch和torchvision官方文档使用方法
su直接切换到超级管理员模式,这样很多报错都可以避免了
Uniapp壁纸小程序源码/双端微信抖音小程序源码
The R language uses the tablestack function of epidisplay package to generate statistical analysis tables based on grouped variables (including descriptive statistical analysis, hypothesis test, diffe
(六)控制语句if/else switch
Basic knowledge of stock introduction: is fixed increase good or bad?
The safety of link 01 was questioned, and "ultra high strength" became "high strength"_ Publicity_ Steel_ problem
男神女神投票源码 v5.5.21 投票源码
The significance of writing technology blog
STL——函数对象