Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop through an array in vlang

Tags:

loops

vlang

How can I loop over an array of strings on v programming language?
For example: langs := ['python', 'java', 'javascript']

like image 948
MosheZada Avatar asked Jul 09 '19 12:07

MosheZada


People also ask

Can you loop through an array?

You can loop through the array elements with the for loop, and use the length property to specify how many times the loop should run.

Is Vlang faster than C?

V's official documentation claims that it is as fast as C, with the least amount of allocations and an in-built serialization without any run-time reflection.

What is looping through an array called?

We can use iteration with a for loop to visit each element of an array. This is called traversing the array. Just start the index at 0 and loop while the index is less than the length of the array.

What is Vlang written in?

Vlang (V) is a statically typed programming language inspired by Rust, Go, Oberon, Swift, Kotlin, and Python. It is open-sourced and claims to be very easy to learn. Based on the V documentation: “Going through this documentation will take you about an hour, and by the end, you will have learned the entire language.”


2 Answers

Method 1: For loop with index

langs := ['python', 'java', 'javascript']

for i, lang in langs {
    println('$i) $lang')
}                             

Method 1 Output:

0) python
1) java
2) javascript

Try method 1 on vlang's playground here

Method 2: For loop without index

langs := ['python', 'java', 'javascript']

for lang in langs {
    println(lang)
}      

Method 2 Output:

python
java
javascript

Try method 2 on vlang's playground here

Method 3: While loop style iteration using for in V Lang You can do this too. Following loop is similar to while loop in other languages.

mut num := 0
langs := ['python', 'java', 'javascript']

for{
    if num < langs.len {
        println(langs[num])
    }
    else{
        break
    }
    num++
}

Method 3 Output:

python
java
javascript

Try method 3 on vlang's playground here

Method 4: Looping over elements of an array by accessing its index

langs := ['python', 'java', 'javascript']

mut i := 0
for i < langs.len {
    println(langs[i])
    i++
}

Method 4 Output:

python
java
javascript

Try method 4 on V lang's playground here

Method 5: Traditional C-Style looping

As suggested by @Astariul in the comments

langs := ['python', 'java', 'javascript']

for i := 0; i < langs.len; i++ {
    println(langs[i])
}

Method 5 Output:

python
java
javascript

Try method 5 on V lang's playground here

You can checkout this playlist for more interesting vlang tutorials

like image 90
navule Avatar answered Oct 22 '22 05:10

navule


V has only one looping construct: for.
In order to loop over the array langs, you need to use the for loop.

langs := ['python', 'java', 'javascript']
for lang in langs {
    println(lang)
}

The for value in loop is used for going through elements of an array. If an index is required, an alternative form for index, value in can be used.

like image 39
MosheZada Avatar answered Oct 22 '22 03:10

MosheZada