Next Continue in Julia

Find this useful? Support us: Star on GitHub 6
Category: Loop | Language: Julia

In Julia, next and continue are control flow statements that allow you to skip over certain iterations of a loop.

continue: This statement allows you to skip the current iteration of a loop and move on to the next iteration. Here's an example:

for i in 1:10
    if i % 2 == 0
        continue
    end
    println(i)
end

# Output:
# 1
# 3
# 5
# 7
# 9

In the example above, we are printing out the values from 1 to 10, but we only print the ones that are not divisible by 2. This is achieved using the continue statement, which skips over the even numbers and moves on to the next odd number.

next: This statement works similarly to continue in that it allows you to skip over certain iterations of a loop. However, where continue skips the current iteration, next allows you to skip any arbitrary iteration. Here's an example:

for i in 1:10
    if i == 5
        next
    end
    println(i)
end

# Output:
# 1
# 2
# 3
# 4
# 6
# 7
# 8
# 9
# 10

In this example, we are printing out the values from 1 to 10 again, but this time we skip over the value of 5 using the next statement.