If you need to executes code block repeatedly until condition satisfies, For and while loop are used.

Let’s talk about while loop in Julia

Julia while loop examples

while loop used to execute a repeated code blocks until an condition satisfies

while conditional_statement
    // code block statements
end

conditional_statement is an conditional expression, always returns Bool value. If value is true, It executes code block statements.

Here is an example

k = 1;
while k <= 5
  println(k)
  global k=k+1
end

Output prints values from 1 to 5.

While infinite loop:

Also, if conditional expression is true and there is no way to break the while, It is in infinite loop.

k = 1;
while true
  println(k)
end

Break and continue in while loop examples

break: used to break the loop from execution

here is an break usage in while loop

k = 1;
while k<=10
  println(k)

  if k == 3
    break
  end
  global k=k+1
end

Without break, It prints the values from 1 to 10. with break, It prints 1,2,3 values

continue in while loop example: continue used to continue the loop execution by skipping the code after continue block.

k = 1;
while k<=10
  global k=k+1
 if k % 3 != 0
    continue
  end   
    println(k)

end

Output:

3
6
9