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

Repeating (the for loop)

This lesson covers looping in Julia with a for statement, with the aim of using ranges to run work efficiently.

You can repeat using a range such as for i in 1:5. You can also loop straight over an array or collection, handling the elements without thinking about indexes.

The sample code uses the range for i in 1:5 to print numbered messages, closing the loop with end. The 1:5 range literal is Julia's concise idiom.

A common early stumble is that Julia's ranges include both ends. 1:5 means 1 through 5 inclusive, which differs from the convention in many languages of excluding the upper bound.

In professional work, the for loop is one of the most frequently used constructs there is, for handling the contents of an array one at a time.

๐Ÿ“– 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)