HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to ElixirVariablesData TypesOperatorsStringsListsConditions (if / else)Iteration (Enum)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables & Types
🔢 Numbers & Math
✍️ String
📚 List & Enum
🔀 Condition
🧩 Function
Elixir›Conditions (if / else)

Conditions (if / else)

Decision making with if...do...else...end and logical operators.

Simple enough for a 5-year-old to understand

decision with if

Example code
1
age = 20
2
if age >= 18 do
a block is opened with do.
Conditiona block is opened with do.
3
IO.puts("Adult")
4
else
5
IO.puts("Child")
6
end
Blocks end with end.
ConditionBlocks end with end.
Condition
💡 Hint: the if ... do ... else ... end structure is used.

About this topic

Conditional structures in Elixir are expressions and produce values. if, unless, cond, and case are used for this purpose. Which one you choose depends on what the branching is based on.

The truthiness rule is simple: only false and nil are falsey, everything else is truthy. Zero, an empty string, and an empty list are all considered truthy. This is different from many languages and leads to silent bugs if you aren't careful.

There is no "else if" structure in Elixir. When you want to try multiple conditions, you use cond; when you want to branch based on the shape of a value, you use case. Knowing the distinction between the two keeps your code readable.

What you will learn

  • if and unless structures
  • Truthiness rules
  • Using cond
  • Pattern matching with case
  • Conditions producing values

Who is this for?

  • Learners of control flow
  • Developers looking for else if
  • Those transitioning to pattern matching

When to use?

If there is a single condition, if is enough; if multiple independent conditions are to be tried, cond is readable.

If you are branching based on the structure of a value, case is the right tool and allows you to unpack parts at the same time.

Common mistakes

Wrong

sayi = 5
if sayi > 10 do
  IO.puts "big"
else if sayi > 3 do
  IO.puts "medium"
end

There is no else if structure in Elixir; this code results in a syntax error. To try multiple conditions, cond is used. Those who don't know the structure try this syntax out of habit from other languages, and the error message isn't very descriptive either.

Correct

sayi = 5
cond do
  sayi > 10 -> IO.puts "big"
  sayi > 3 -> IO.puts "medium"
  true -> IO.puts "small"
end

Wrong

liste = []
if liste do
  IO.puts "full"
end

An empty list is considered truthy in Elixir; the block runs and the wrong message is printed to the screen. Only false and nil are falsey. You need to write an explicit check to test for emptiness.

Correct

liste = []
if liste != [] do
  IO.puts "full"
end

Wrong

sayi = 5
cond do
  sayi > 10 -> "big"
  sayi > 100 -> "very big"
end

If no condition is met, the program stops with a CondClauseError; additionally, the second branch is logically never reached because the narrower condition was written later. A cond structure must always have a catch-all branch and conditions should be ordered from narrowest to widest.

Correct

sayi = 5
cond do
  sayi > 100 -> "very big"
  sayi > 10 -> "big"
  true -> "small"
end

Frequently asked questions

What is the difference between cond and case?

cond evaluates conditions and picks the first true one; case matches a value against patterns. If you are doing comparisons, cond is right; if you are looking at structure, case is right.

Does if return a value?

Yes, it returns the last expression of the executing branch. If no condition is met and there is no else branch, it returns a nil value. That's why you can directly bind the result of an if to a name.

What does the with structure do?

It chains consecutive matches that must each succeed. If any of them doesn't match, the chain stops and that value is returned. Because it eliminates nested case stacks, it is widely used in error handling.

Summary

Only false and nil are falsey.

There is no else if structure; cond is used instead.

cond always requires a catch-all branch.

case branches based on the structure of the value.

Conditional structures produce values.

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