If you need to execute code block repeatedly until the condition is satisfied, For and while loops are used.
Let’s talk about this while loop in Julia
Julia while loop examples
while loop is used to execute a repeated code block until an condition satisfies
while conditional_statement // code block statements end
conditional_statement is a conditional expression, that always returns the
If the 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 the conditional expression is true and there is no way to break the while, It is in an 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 a break usage in the while loop
k = 1; while k<=10 println(k) if k == 3 break end global k=k+1 end
Without a break, It prints the values from 1 to 10. with a break, It prints 1,2,3 value
continue in while loop example: continue used to continue the loop execution by skipping the code after the continue block.
k = 1; while k<=10 global k=k+1 if k % 3 != 0 continue end println(k) end
3 6 9