当前位置:网站首页>Go语言for循环
Go语言for循环
2022-06-11 20:54:00 【只是六号z】
for语句
循环语句是表示条件满足。可以反复的执行某段代码。
for是唯一的循环,Go中没有while。
for循环的基本用法
语法结构:
for init;condition;post{
}
初始化语句只执行一次。在初始化循环之后,将检查该条件。如果条件计算为true,那么中的循环体将被执行,然后是post语句。post语句将在循环的每次成功迭代之后执行。在执行post语句之后,该条件将被重新检查。如果它是正确的,循环将继续执行,否则循环终止。
for循环的其他写法
for循环的所有的三个组成部分,都是可选的,即初始化、条件和post都是可选的。
(1)同时省略表达式1和表达式3,相当于while循环
for 表达式2{
}
(2)for循环的range格式可以对slice、map、数组、字符串进行迭代循环。
for key,value := range oldMap{
newMap[key] = value
}
(3)同时省略三个表达式,相当于while(true),相当于直接作用在true上。
for {
}
for循环练习
练习题一:打印18-23等数字
package main
import "fmt"
func main() {
for i := 58; i >= 23 ; i-- {
fmt.Println(i)
}
}
练习题二:求1-100的和
package main
import "fmt"
func main() {
sum := 0
for i := 0; i <= 100 ; i++ {
sum += i;
}
fmt.Println(sum)
}
练习题三:打印1-100内,能够被3整除,但是不能被5整除的数字,统计被打印数字的个数,每行打印五个。
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("需要打印的个数有",count,"个")
}
多层循环嵌套
练习题一:打印*,每行五个,一共五行。
package main
import "fmt"
func main() {
for i := 0; i < 5; i++ {
for j := 0; j < 5; j++ {
fmt.Printf("*")
}
fmt.Println()
}
}
练习题二:打印九九乘法表
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()
}
}
边栏推荐
- 应用场景:现场直播节目制作NDI技术中PoE网卡的广泛应用
- Serval and Rooted Tree(CF1153D)-DP
- On scale of canvas recttransform in ugui
- BCC tool tool usage
- Tree Basics
- 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
- MySQL add adds multiple new fields and specifies the field location
- MySQL installation free configuration tutorial under Windows mysql-5.6.51-winx64 Zip version
- Current situation and future development trend of global and Chinese cogeneration system market from 2022 to 2028
- Mysql add 新增多个新字段并指定字段位置
猜你喜欢

Frequency domain filter

IDEA中,运行yarn命令,显示无法加载文件,因为在此系统上禁用运行脚本

全球机器视觉市场规模持续上涨,PoE图像采集卡为工业相机提供高速传输通道

Why is your LDO output unstable?

Docker installing MySQL

Black circle display implementation

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

从概率论基础出发推导卡尔曼滤波

Chinese text classification based on CNN

26. timer
随机推荐
sql优化之DATE_FORMAT()函数
桌面以及文件资源管理器无限重启的解决
2022-2028 global and Chinese thermopile detector Market Status and future development trend
php pcntl_ Fork create multiple child process resolution
Frequency domain filter
[nk] deleted number of 100 C Xiaohong in Niuke practice match
c语言程序设计知识点总结 01
Research and Analysis on the market status of polybutene-1 in China from 2021 to 2027 and forecast report on its development prospect
为什么100G网络传输要使用iWARP、RoCE v2、NVMe-oF等协议
2022-2028 current situation and future development trend of global and Chinese thermocouple belt Market
13 r basic exercises
MySQL installation free configuration tutorial under Windows mysql-5.6.51-winx64 Zip version
周刊02|不瞒你说,我其实是MIT的学生
What is the essence and process of SCM development? Chengdu Automation Development Undertaking
Log in with password and exit with error for three times.
2022-2028 global and Chinese thermocouple sensor market status and future development trend
First modelarts training
Figure guessing game
Solution to the problem of PHP strtotime obtaining natural monthly error
Rtd2171u, substitute for trd2171u, substitute for trd2171u, cs5261 C to hdmi4k_ 30Hz