Search…
V by Example
en
V por exemplos
V mit einem Beispiel
V dengan Contoh
通过例子学V语言
例子
section_3
section_1
section_4
section_2
运算符
循环结构
If表达式
匹配
コード例で学ぶV言語
Changelog
Contributing
Documentation Style Guide
Powered By
GitBook
循环结构
在V语言中只有一种类型的循环,像Go一样,它可以在很多方面使用。
for
循环
for
循环提供了一种快速而简单的方法来重复做一些事情。 如果你想一次又一次地运行相同的代码,每次使用不同的值,它们都很方便。 你可以把一个循环想象成一个电脑版的游戏,告诉某人朝一个方向走X步,然后朝另一个方向走Y步; 例如,“向东走五步”可以用循环的方式来表达:
1
for
i
:=
0
;
i
<
5
;
i
++
{
2
println
(
'Walking one step'
)
3
}
Copied!
V有
for
循环结构,循环可以用不同的方式编写:
数组/映射的
in
运算符
1
ages
:=
[
18
,
25
,
32
,
43
,
50
]
2
3
for
age in ages
{
4
println
(
age
)
5
}
Copied!
注意:该值是只读的。
带条件的
for
循环
这是一个控制流语句,允许基于给定的布尔条件重复执行代码。 条件周围没有括号,并且始终需要大括号。
1
mut factorial
:=
1
2
mut counter
:=
1
3
4
for
{
5
counter
++
6
if
counter
>
5
{
7
println
(
factorial
)
8
break
9
}
10
factorial
=
factorial
*
counter
11
}
12
13
println
(
counter
)
Copied!
输出
1
120
2
6
Copied!
带有break语句的for循环总是可以缩短代码,方法是将逆条件放在for之后,使其与其他语言中的while语句等价。
1
mut factorial
:=
1
2
mut counter
:=
1
3
4
for
counter
<=
5
{
5
factorial
=
factorial
*
counter
6
counter
++
7
}
8
println
(
factorial
)
9
println
(
counter
)
Copied!
输出
1
120
2
6
Copied!
传统的C风格
1
mut factorial
:=
1
2
mut counter
:=
1
3
4
for
counter
=
1
;
counter
<
6
;
counter
++
{
5
factorial
=
factorial
*
counter
6
if
counter
==
5
{
7
print
(
factorial
)
8
continue
9
}
10
println
(
counter
)
11
}
Copied!
无限循环
for
循环也能无限循环
1
for
{
2
println
(
'foo'
)
3
}
Copied!
练习
1.写一个V程序来显示前10个自然数。 2.编写一个V程序,找出前10个自然数的和。 3.编写一个V程序来打印数组中的整数,并打印它们的平均值。 4.编写一个V程序,从键盘上读出10个数字,求出它们的和和和平均值。 5.编写一个V程序来显示一个整数之前的数的立方。
Previous
运算符
Next
If表达式
Last modified
2yr ago
Copy link
Contents
for循环
练习