LogoLogo
  • V by Example
  • en
    • examples
      • section_3
        • Functions
        • Methods
        • Arrays
        • Struct
      • section_1
        • Primitives
        • Keywords
        • Variables
        • Formatted Print
        • Strings
        • Comments
      • section_4
        • Files
        • JSON
        • Testing
        • Array Functions
      • section_2
        • Operators
        • Looping Constructs
        • If statement
        • Match
  • V por exemplos
    • Contribuindo
    • examples
      • section_1
        • Comentários
        • Primitivos
        • Palavras-chave
      • section_2
        • Operadores
  • V mit einem Beispiel
    • examples
      • section_3
        • Methods
        • Struct
        • Functions
        • Arrays
      • section_4
        • Testing
        • Array Functions
        • JSON
        • Files
      • section_2
        • Looping Constructs
        • If statement
        • Match
        • Operators
      • section_1
        • Primitive Datentypen
        • Schlüsselwörter
        • Formatted Print
        • Strings
        • Kommentare
        • Variables
    • CONTRIBUTING_de
  • V dengan Contoh
    • examples
      • section1
        • Variabel
  • 通过例子学V语言
    • 例子
      • section_3
        • 函数
        • 方法
        • 数组
        • 结构体
      • section_1
        • 基本类型
        • 关键字
        • 变量
        • print系列函数
        • 字符
        • 注释
      • section_4
        • 文件
        • JSON操作
        • 调试
        • 数组函数
      • section_2
        • 运算符
        • 循环结构
        • If表达式
        • 匹配
  • コード例で学ぶV言語
    • examples
      • section_3
        • 関数
        • メソッド
        • 配列
        • 構造体
      • section_1
        • プリミティブ型
        • キーワード
        • 変数
        • 書式付き出力
        • 文字列
        • コメント
      • section_4
        • ファイル操作
        • JSON操作
        • テスト
        • 配列の関数
      • section_2
        • 演算子
        • ループ
        • If文
        • マッチ
  • Changelog
  • Contributing
  • Documentation Style Guide
Powered by GitBook
On this page
  • for循环
  • 练习

Was this helpful?

  1. 通过例子学V语言
  2. 例子
  3. section_2

循环结构

在V语言中只有一种类型的循环,像Go一样,它可以在很多方面使用。

for循环

for循环提供了一种快速而简单的方法来重复做一些事情。 如果你想一次又一次地运行相同的代码,每次使用不同的值,它们都很方便。 你可以把一个循环想象成一个电脑版的游戏,告诉某人朝一个方向走X步,然后朝另一个方向走Y步; 例如,“向东走五步”可以用循环的方式来表达:

for i := 0; i < 5; i++ {
    println('Walking one step')
}

V有for循环结构,循环可以用不同的方式编写:

  • 数组/映射的in运算符

ages := [18, 25, 32, 43, 50]

for age in ages {
    println(age)
}

注意:该值是只读的。

  • 带条件的for循环

这是一个控制流语句,允许基于给定的布尔条件重复执行代码。 条件周围没有括号,并且始终需要大括号。

mut factorial := 1
mut counter := 1

for {
    counter++
    if counter > 5 {
       println(factorial)
       break
    }
    factorial = factorial * counter
}

println(counter)

输出

120
6

带有break语句的for循环总是可以缩短代码,方法是将逆条件放在for之后,使其与其他语言中的while语句等价。

mut factorial := 1
mut counter := 1

for counter <= 5 {
    factorial = factorial * counter
    counter++
}
println(factorial)
println(counter)

输出

120
6
  • 传统的C风格

mut factorial := 1
mut counter := 1

for counter = 1; counter < 6; counter++ {
    factorial = factorial * counter
    if counter == 5 {
        print(factorial)
        continue
    }
    println(counter)
}
  • 无限循环

for循环也能无限循环

for {
    println('foo')
}

练习

1.写一个V程序来显示前10个自然数。 2.编写一个V程序,找出前10个自然数的和。 3.编写一个V程序来打印数组中的整数,并打印它们的平均值。 4.编写一个V程序,从键盘上读出10个数字,求出它们的和和和平均值。 5.编写一个V程序来显示一个整数之前的数的立方。

Previous运算符NextIf表达式

Last updated 5 years ago

Was this helpful?