Ad space (banner)
๐ŸŸฃJulia Lessons
Lesson 8 / 68

Repeating (the while loop)

This lesson covers looping in Julia with a while statement, with the aim of choosing correctly between it and a for loop.

while ... end repeats for as long as the condition holds. The basic rule of thumb: a for loop when you already know the finishing condition, a while loop when you do not.

The sample code initialises with i = 1, repeats while i <= 5, and increases the value by one inside the loop with global i += 1. Note the global keyword, needed to update a variable in the global scope.

A common early stumble is exactly that need for global. Updating a top-level variable from inside a loop requires it in Julia, which is different from most languages.

In professional work, while loops are used where the number of repetitions is not known in advance — repeating until some condition is met.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Julia directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)