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 loop
  • Exercises

Was this helpful?

  1. V mit einem Beispiel
  2. examples
  3. section_2

Looping Constructs

There's only one type of loop in V language, like Go which can be used in many ways.

for loop

for loops offer a quick and easy way to do something repeatedly. They're handy, if you want to run the same code over and over again, each time with a different value. You can think of a loop as a computerized version of the game where you tell someone to take X steps in one direction then Y steps in another; for example, the idea "Go five steps to the east" could be expressed this way as a loop:

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

V has the for looping construct and the loop can be written in different ways:

  1. in operator for array/map

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

for age in ages {
    println(age)
}

Note: The value is read-only.

  1. for loop with a condition

This is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. There are no parentheses surrounding the condition, and the braces are always required.

mut factorial := 1
mut counter := 1

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

println(counter)

Output

120
6

A for loop with a break statement can always be made shorter by placing the inverse condition right after for, making it equivalent with the while statement in other languages.

mut factorial := 1
mut counter := 1

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

Output

120
6
  1. Traditional C style

mut factorial := 1
mut counter := 1

for counter = 1; counter < 6; counter++ {
    factorial = factorial * counter
    if counter == 5 {
        print(factorial)
        continue
    }
    println(counter)
}
  1. Infinite Loop

for loop can also be infinite

for {
    println('foo')
}

Exercises

  1. Write a V program to display the first 10 natural numbers.

  2. Write a V program to find the sum of first 10 natural numbers.

  3. Write a V program to print the integers inside an array and also print their mean.

  4. Write a V program to read 10 numbers from keyboard and find their sum and average.

  5. Write a V program to display the cube of the number upto given an integer.

Previoussection_2NextMatch

Last updated 5 years ago

Was this helpful?