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. examples
  3. section_2

ループ

V言語のループ構文は1種類しかありません。Goでもこのループ構文は広く使われています。

forループ

forループは、何かを繰り返すための簡単かつ便利な方法を提供します。 値を変えながら同じコードを繰り返し実行するのも簡単です、 ループについて考えるために、ゲームをコンピュータ化するときに人物をある方向にX歩進め、次に別の方向にY歩進める状況を考えてみましょう。 たとえば「東に5歩進め」は以下のようにループで表現できます。

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

Vのforループは、さまざまな方法で構成できます。

  • arrayやmapでin演算子を使う

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

for age in ages {
    println(age)
}

注意: 値はリードオンリーです。

  • forループで条件を指定する

これは、指定されたbool条件に応じてコードを繰り返し実行する制御フローを記述します。 条件部分を丸かっこで囲む必要はありませんが、波かっこ{ }は常に必要です。

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)

Output

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. 自然数の最初の10個を表示するVプログラムを書きましょう。

  2. 自然数の最初の10個の合計を求めるVプログラムを書きましょう。

  3. arrayの中にあるintegerを表示し、それらの平均も表示するVプログラムを書きましょう。

  4. キーボード入力から10個の数値を読み込み、それらの合計と平均を求めるVプログラムを書きましょう。

  5. 渡されたintegerの立方数(3乗)を表示するVプログラムを書きましょう。

Previous演算子NextIf文

Last updated 5 years ago

Was this helpful?