HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to JuliaVariablesData TypesOperatorsStringsArraysConditions (if / else)Loops (for / while)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables & Types
🔢 Numbers & Math
✍️ String
📚 Array & Dictionary
🔀 Condition & Loop
🧩 Function
Julia›Loops (for / while)

Loops (for / while)

Iteration with for using ranges (1:5), conditional repetition with while, and the global scope rule at the top level.

Simple enough for a 5-year-old to understand

for loop

The 1:3 range is from 1 to 3 (3 INCLUDED). The block ends with end.

Example code
1
for i in 1:3
1, 2, 3 (3 included).
Loop1, 2, 3 (3 included).
2
println(i)
3
end
Loop
💡 Hint: 1:3 → 3 is included (unlike range in Python).

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.Print the numbers from 1 to 5 vertically using a for loop.

🎯 Expected output: 1 2 3 4 5

Loading editor…

About this topic

Loops in Julia are written with for and while. The for loop iterates over a range or collection; both the start and end of ranges are inclusive. This requires attention for those coming from zero-indexed languages.

Loops in Julia are fast. In many interpreted languages, loops are avoided in favor of vector operations; thanks to compiled code in Julia, writing explicit loops is just as fast. This is a great freedom in terms of readability.

It is important to remember that the loop has its own scope. Variables defined inside are not visible from the outside, and updating an external variable requires an explicit declaration. Wrapping the code in a function completely eliminates this issue.

What you will learn

  • the for loop and ranges
  • The inclusion of bounds
  • the while loop
  • Using break and continue
  • Loop scope

Who is this for?

  • Researchers writing numerical loops
  • Those who think the upper bound is exclusive
  • Those getting scope errors

When to use?

In iterative calculations where each step depends on the previous one, a loop is inevitable and does not cause a performance loss in Julia.

For independent element transformations, broadcasting is written more concisely; the performance difference between the two is usually small.

Common mistakes

Wrong

dizi = [10, 20, 30]
for i in 0:length(dizi)
    println(dizi[i])
end

Both the start is wrong and the upper bound is too high: indices start at one and both ends of the range are inclusive. At the first step, the zeroth element is requested and a BoundsError is thrown. The correct range for array indices is from one to the length.

Correct

dizi = [10, 20, 30]
for i in 1:length(dizi)
    println(dizi[i])
end

Wrong

i = 1
while i <= 5
    println(i)
end

Since the counter is not incremented, the condition remains true forever and the program never ends. You have to ensure progress in while loops; using for when the number of iterations is known eliminates this risk.

Correct

i = 1
while i <= 5
    println(i)
    global i += 1
end

Wrong

for i in 5:1
    println(i)
end

Since the start is greater than the end, the range is empty and the loop never runs. You don't even get an error message; the program silently does nothing, making it hard to notice. To count backwards, a step must be specified.

Correct

for i in 5:-1:1
    println(i)
end

Frequently asked questions

How to specify a step?

The step value is written in the middle of the range; start, step, and end come in that order. A negative step allows counting backwards. This syntax is the same as MATLAB and stems from the scientific computing tradition.

How do I get the index and element together?

The enumeration function provides the sequence number and value together at each step. It prevents you from accessing by index and eliminates the risk of out-of-bounds errors.

How are nested loops written?

You can write them separately or combine them in a single for statement with a comma. The combined syntax opens a single block and reduces indentation. For matrix operations, a loop arrangement suited to column-major ordering is faster.

Summary

Both ends of ranges are inclusive.

Indices start at one.

A reverse range is empty; a step must be specified.

You must ensure progress in a while loop.

The loop opens its own scope.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code