当前位置:网站首页>Go language for loop
Go language for loop
2022-06-11 21:04:00 【Just number six Z】
Go Language for loop
for sentence
A loop statement means that a condition is satisfied . You can execute a piece of code repeatedly .
for Is the only loop ,Go There is no while.
for The basic use of loops
Grammatical structure :
for init;condition;post{
}
The initialization statement is executed only once . After initializing the loop , This condition will be checked . If the condition is calculated as true, Then the loop body in will be executed , And then there was post sentence .post Statement will be executed after each successful iteration of the loop . In execution post After statement , This condition will be rechecked . If it's right , The loop will continue , Otherwise the cycle ends .
for Other ways to write a loop
for All three components of the loop , It's all optional , Initialization 、 Conditions and post It's all optional .
(1) Also omit the expression 1 And expressions 3, amount to while loop
for expression 2{
}
(2)for Cyclic range The format can be right slice、map、 Array 、 String to iterate over .
for key,value := range oldMap{
newMap[key] = value
}
(3) Omit three expressions at the same time , amount to while(true), Equivalent to acting directly on true On .
for {
}
for Circulation practice
Exercise one : Print 18-23 Equal number
package main
import "fmt"
func main() {
for i := 58; i >= 23 ; i-- {
fmt.Println(i)
}
}
Exercise 2 : seek 1-100 And
package main
import "fmt"
func main() {
sum := 0
for i := 0; i <= 100 ; i++ {
sum += i;
}
fmt.Println(sum)
}
Exercise 3 : Print 1-100 Inside , It can be 3 to be divisible by , But can't be 5 Divisible numbers , Count the number of printed numbers , Print five per line .
package main
import "fmt"
func main() {
count := 0
for i := 1; i <= 100; i++ {
if i % 3 == 0 && i % 5 != 0 {
count ++
fmt.Print(i,"\t")
if count % 5 == 0 {
fmt.Println()
}
}
}
fmt.Println()
fmt.Println(" The number to be printed is ",count," individual ")
}
Multi level loop nesting
Exercise one : Print *, Five per line , Five lines altogether .
package main
import "fmt"
func main() {
for i := 0; i < 5; i++ {
for j := 0; j < 5; j++ {
fmt.Printf("*")
}
fmt.Println()
}
}
Exercise 2 : Print the multiplication table
package main
import "fmt"
func main() {
for i := 1; i <= 9; i++ {
for j := 1; j <= i; j++ {
fmt.Printf("%d x %d = %d\t",j,i,i*j)
}
fmt.Println()
}
}
边栏推荐
- Space transcriptome experiment | what factors will affect the quality of space transcriptome sequencing during the preparation of clinical tissue samples?
- 周刊02|不瞞你說,我其實是MIT的學生
- [data visualization] Apache superset 1.2.0 tutorial (III) - detailed explanation of chart functions
- Tensorflow 2. X Getting Started tutorial
- 技术交流|网络安全设备为什么要用Bypass功能
- Log in with password and exit with error for three times.
- Live broadcast with practice | 30 minutes to build WordPress website with Alibaba cloud container service and container network file system
- 【博弈论-完全信息静态博弈】 战略式博弈
- Go语言条件语句
- 成长的12条黄金法则
猜你喜欢

Pyqt5 technical part - set the default value of qcombobox drop-down box and get the current selection of the drop-down box

php pcntl_fork 创建多个子进程解析

Explanation of each column output by explain statement

Obsidian关系图谱如何让节点可以手动拖动

频域滤波器

Windows icon display exception resolution. The desktop icon is abnormal, the start menu icon is abnormal, and the taskbar icon is abnormal. Icon cache location.

unity package manager starting server stuck(Unity啟動卡在starting server,然後報錯)

One article to show you how to understand the harmonyos application on the shelves

Application scenario: wide application of Poe network card in NDI technology for live broadcast program production

PHP strtotime 获取自然月误差问题解决方案
随机推荐
Cuckoo Hash
ASCII码对照表
unity package manager starting server stuck(Unity启动卡在starting server,然后报错)
LR-LINK联瑞携新品首次亮相数博会-助力新基建数据中心建设
ASCII code comparison table
RANSAC提取平面(MATLAB内置函数)
机器视觉工控机PoE图像采集卡应用解析
Yintai department store and Taobao tmall jointly create a green fashion show to help "carbon neutrality"
Unity package manager starting server stuck
RANSAC提取圆柱(MATLAB内置函数)
Final examination of Dialectics of nature 1
Figure guessing game
unity package manager starting server stuck(Unity啟動卡在starting server,然後報錯)
Why should I use iwarp, roce V2, nvme of and other protocols for 100g network transmission
New product release: domestic single port Gigabit network card is officially mass produced!
gateway先启动其他微服务,在启动网关,网关启动不了,且无异常日志;先启动网关,所有服务能正常启动
Release of version 5.6 of rainbow, add multiple installation methods, and optimize the topology operation experience
Pyqt5 technical part - cause of the problem that setting the top of the window does not take effect. Setwindowflags() does not take effect after setting the parameters. Solution
周刊02|不瞞你說,我其實是MIT的學生
Log in with password and exit with error for three times.