for loop used to iterate objects, executes the code block repeatedly. objects are iterable such as arrays, dictionaries, sets, and strings.

Julia For loop examples

There are multiple variations of the for loop in Julia

  • for in-loop
array = [1,2,3,4]
for i in array
  println(array[i])
end
1
2
3
4

Here is a string array iteration

names = ["one", "two", "three"];

for name in names
  println(" $name.")
end

Output:

one
two
three

Continue in for loop

continue is a keyword used to continue loop execution and statements after continue are skiped and executes the next iteration.

Here is an example

for j in 1:5
    if j%2 == 0
        continue
    end
    println(j)
end

How to iterate for loop with index

The enumerate function returns a tuple of index and value. use for in loop to iterate each iteration.

Here is an example

array = [11,21,31,41]
for(index,value) in enumerate(array)
  println("$index $value")
end

Output:

1 11
2 21
3 31
4 41