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›Conditions (if / else)

Conditions (if / else)

Decision making with if-elseif-else; conditions without parentheses, blocks ending with end.

Simple enough for a 5-year-old to understand

decision with if

In Julia, conditions do not require parentheses; blocks end with end.

Example code
1
age = 20
2
if age >= 18
Condition without parentheses.
ConditionCondition without parentheses.
3
println("Adult")
Runs if true.
Output / InputRuns if true.
4
else
5
println("Child")
6
end
Blocks end with end.
ConditionBlocks end with end.
ConditionOutput / Input
💡 Hint: Every block (if/for/while/function) is closed with end.

About this topic

In Julia, conditions are written with if, elseif, and else, and blocks are closed with end. The middle branch is written as a single word; if written separately, a new block opens and an extra closing is required.

The conditional expression must evaluate to a boolean. You cannot use a number as a boolean value; Julia explicitly rejects this and throws an error. This strictness is better than code that fails silently.

In Julia, if is an expression and produces a value. The value of the last evaluated expression is returned, so you can assign the result of a condition directly to a variable. The ternary operator is also available and used for short selections.

What you will learn

  • the if, elseif, and else structure
  • The requirement for the condition to be a boolean value
  • Short-circuit operators
  • The ternary operator
  • Conditions producing values

Who is this for?

  • Learners of control flow
  • Those coming from C or Python
  • Those getting type errors in conditions

When to use?

In numeric code, boundary checks and edge case distinctions are made with conditions; incorrect type usage is caught immediately.

Short-circuit operators are used like statements to perform a single operation when a condition is met, and they are common in Julia code.

Common mistakes

Wrong

x = 5
if x
    println("full")
end

Julia requires the condition to be a boolean; when a number is provided, a "TypeError: non-boolean (Int64) used in boolean context" error is thrown. You are expected to write the comparison explicitly. This is the first difference that trips up those coming from Python and C.

Correct

x = 5
if x != 0
    println("full")
end

Wrong

x = 5
if x > 10
    println("large")
else if x > 3
    println("medium")
end

When else and if are written separately, a new block opens and each requires its own closing; this code errors out. When the single-word middle branch is used, no extra closing is needed and the structure remains readable.

Correct

x = 5
if x > 10
    println("large")
elseif x > 3
    println("medium")
end

Wrong

dizi = []
if length(dizi) > 0 || dizi[1] == 5
    println("yes")
end

No error here, but the logic is reversed: the or operator evaluates the right side if the left side is false, so on an empty array the second condition runs and throws an out-of-bounds error. The and operator should be used in guard checks, and the order matters.

Correct

dizi = []
if length(dizi) > 0 && dizi[1] == 5
    println("yes")
end

Frequently asked questions

Is there a switch in Julia?

It does not exist in the standard language. The alternative is an elseif chain or dispatching through a mapping structure. Multiple dispatch also meets part of this need: you write separate function versions for different types.

How are short-circuit operators used as statements?

You write a condition on the left and the action to be performed on the right. If the condition is met, the right side runs. It is common in single-line checks and frequently seen in Julia code; however, overuse reduces readability.

Does if return a value?

Yes, it returns the value of the last expression in the executed branch. This allows you to assign the condition result directly to a variable. If no branch runs, a void value is returned.

Summary

Conditions must be boolean values.

The middle branch is written as a single word.

Blocks are closed with end.

The order of short-circuit operators matters.

if is an expression and produces a value.

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