Looping Constructs
There's only one type of loop in V language, like Go which can be used in many ways.
for
loop
for
loopfor
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:
V has the for
looping construct and the loop can be written in different ways:
in
operator for array/map
Note: The value is read-only.
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.
Output
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.
Output
Traditional C style
Infinite Loop
for
loop can also be infinite
Exercises
Write a V program to display the first 10 natural numbers.
Write a V program to find the sum of first 10 natural numbers.
Write a V program to print the integers inside an array and also print their mean.
Write a V program to read 10 numbers from keyboard and find their sum and average.
Write a V program to display the cube of the number upto given an integer.
Last updated