Control Flow in Odin
Learn conditionals, switch statements, and loops in Odin -- if/else, switch with ranges, the unified for loop, and loop control with Docker-ready examples
Control flow determines the order in which statements execute – which branches run, how often code repeats, and when a loop stops. As an imperative, procedural language, Odin handles control flow with familiar structured constructs, but it sands off many of C’s rough edges in the process.
Two design choices stand out. First, Odin has a single loop keyword – for – that covers every kind of iteration, from C-style counting to range iteration to infinite loops. There is no separate while keyword; a for with just a condition is the while loop. Second, Odin’s switch does not fall through between cases by default, eliminating a classic source of C bugs. You opt into fall-through explicitly with fallthrough.
In keeping with Odin’s “explicit over implicit” philosophy, conditions are not wrapped in parentheses, blocks always require curly braces, and there is no hidden control flow. This tutorial walks through conditionals, switch statements, the unified for loop, and loop control statements like break and continue.
Conditionals: if, else, and the ternary
Odin’s if statement needs no parentheses around the condition, but the body always uses braces. A handy feature is the initialization statement: you can declare a variable scoped only to the if/else chain.
Create a file named conditionals.odin:
| |
The initialization statement (score := temperature * 5;) runs once before the condition is tested, and score is only visible inside the if/else. This keeps short-lived helper variables out of the surrounding scope. The ternary operator cond ? a : b returns one of two values inline.
Switch statements
Odin’s switch is more capable than C’s. Cases do not fall through automatically, a single case can match a list of values, and cases can match ranges. The default case is written simply as case: with no value.
Create a file named switch.odin:
| |
The third form – switch with no expression – evaluates each case as a boolean condition, top to bottom. It is Odin’s idiomatic replacement for a long if/else if chain and is often clearer.
The unified for loop
Odin has exactly one loop keyword: for. It takes several shapes depending on how many clauses you give it.
Create a file named loops.odin:
| |
A for with three clauses is the classic counting loop. Drop the init and post clauses and keep only the condition, and you have a while loop. The for x in start..<end form iterates over a numeric range without manual index bookkeeping. (An empty for { } with no clauses at all is an infinite loop.)
Loop control: break, continue, and labels
break exits a loop early and continue skips to the next iteration. Odin also supports labeled loops, letting you break out of an outer loop from within a nested one. Range loops can yield both the value and its index when iterating over collections.
Create a file named loop_control.odin:
| |
When iterating with for value, index in collection, Odin gives you both the element and its position – no separate counter to manage. The label outer: attached to the outer loop lets break outer terminate both loops in one statement, which would otherwise require a flag variable in C.
Running with Docker
Each example is a standalone package main program, so run them one at a time. The file is copied to /tmp (where the compiler has write access for the output binary), and the -file flag tells Odin to compile that single file rather than the whole directory.
| |
Expected Output
Running conditionals.odin:
It's mild
High score: 90
Temperature is above 15
Running switch.odin:
Midweek
Grade: B
negative
Running loops.odin:
Counting up:
0
1
2
Countdown:
3
2
1
Range 0..<3:
0
1
2
Range 1..=3:
1
2
3
Running loop_control.odin:
numbers[0] = 10
numbers[1] = 20
numbers[2] = 30
numbers[3] = 40
numbers[4] = 50
Odd numbers only:
1
3
5
7
9
Stop at 5:
1
2
3
4
5
Nested with label:
(0, 0)
(0, 1)
(0, 2)
(1, 0)
(1, 1)
Key Concepts
- No parentheses around conditions -
if x > 0 { }is the Odin style; braces are always required around the body. - Initialization statements -
if init; condition { }scopes a helper variable to just theif/elsechain, keeping the surrounding scope clean. - One loop keyword -
forcovers C-style loops, while loops (condition only), range loops (for i in 0..<n), and infinite loops (for { }). There is no separatewhile. - Range operators -
..<is half-open (excludes the end) and..=is inclusive (includes the end); both work inforranges andswitchcases. - Switch never falls through - Cases break automatically, a single case can list multiple values, and a bare
switchevaluates boolean cases like an if/else chain. Usefallthroughonly when you explicitly want C-style behavior. - Value-and-index iteration -
for value, index in collectionhands you both without a manual counter. - Labeled loops - Attach a label like
outer:to a loop sobreak outerorcontinue outercan target it from inside nested loops. - Ternary expressions -
cond ? a : bselects between two values inline for simple cases.
Running Today
All examples can be run using Docker:
docker pull primeimages/odin:latest
Comments
Loading comments...
Leave a Comment