当前位置:网站首页>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()
}
}
边栏推荐
- vectorDrawable使用报错
- Docker installation redis
- 13 r basic exercises
- 2022-2028 global and Chinese thermal ionization isotope mass spectrometer (TIMS) market status and future development trend
- Release of version 5.6 of rainbow, add multiple installation methods, and optimize the topology operation experience
- The scale of the global machine vision market continues to rise. Poe image acquisition card provides a high-speed transmission channel for industrial cameras
- Black circle display implementation
- The official announced the launch of Alibaba's 2023 global school recruitment: Technical Posts account for more than 60%
- Lanqi technology joins in, and dragon dragon dragon community welcomes leading chip design manufacturers again
- php pcntl_fork 创建多个子进程解析
猜你喜欢

9 r remove missing values

输入值“18-20000hz”错误,设置信息不完整,请选择单位

Docker installing MySQL

10 R vector operation construction

Which Bluetooth headset is better within 500? Inventory of gifts for girls' Day

Simulate Oracle lock waiting and manual unlocking

频域滤波器
![[data visualization] Apache superset 1.2.0 tutorial (III) - detailed explanation of chart functions](/img/1f/00f2085186971198928b012a3792ea.jpg)
[data visualization] Apache superset 1.2.0 tutorial (III) - detailed explanation of chart functions

Weekly 02 | to tell you the truth, I am actually a student of MIT

moderlarts第一次培训
随机推荐
The secret of derating - don't challenge Datasheet
Research and Analysis on the market status of polybutene-1 in China from 2021 to 2027 and forecast report on its development prospect
What are striplines and microstrip lines? Reference planes and transmission lines
[index system] the latest modeling method of data warehouse index system
JSON introduction
产品资讯|PoE网卡家族集体亮相,机器视觉完美搭档!
黑圆圈显示实现
IDEA中,运行yarn命令,显示无法加载文件,因为在此系统上禁用运行脚本
8 r subset
2022-2028 global and Chinese thermopile and microbolometer infrared detector Market Status and future development trend
Implement AOP and interface caching on WPF client
2022-2028 current market situation and future development trend of thermopile Pyranometers in the world and China
Summary of C language programming knowledge points 01
[nk] deleted number of 100 C Xiaohong in Niuke practice match
Black circle display implementation
周刊02|不瞒你说,我其实是MIT的学生
Current situation and future development trend of hot isostatic pressing service market in the world and China from 2022 to 2028
12 date and time in R
A man always becomes a man and a man always arrives (progress, harvest, growth, self-confidence)
php pcntl_fork 创建多个子进程解析