if as a Statement and Expression
Branch and return values in one form.
if as a Statement and Expression is a free Zig Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Zig Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Branching with if
Use if to choose between paths. Zig runs the first block when the condition is true and skips it otherwise. Simple and explicit. 🌿
if (score > 50) {
std.debug.print("Pass\n", .{});
}Conditions Must Be bool
The condition in parentheses must be a real bool. Zig refuses integers or pointers here, so there is no truthy-or-falsy guessing.
Adding an else
Pair if with else to handle the false case. Exactly one of the two blocks runs, never both and never neither.
if (ready) {
start();
} else {
wait();
}Chaining else if
Stack tests with else if to pick one branch from several. Zig checks them top to bottom and stops at the first match.
if (n < 0) {
sign = -1;
} else if (n > 0) {
sign = 1;
} else {
sign = 0;
}if Is an Expression
In Zig, if can also produce a value. Both branches return something, and the whole if becomes that result you can assign.
const max = if (a > b) a else b;Both Branches Must Match
When if yields a value, both arms must share one type. The compiler reconciles them so the result has a single, predictable type.
No Ternary Needed
Zig has no ternary operator. The expression form of if fills that role cleanly, keeping just one way to write a choice.
Unwrapping an Optional
An if can test and unwrap an optional in one move. The captured name holds the inner value only when it is present.
if (maybe_user) |user| {
greet(user);
}Capturing Errors with if
An if can also peel apart an error union. The else branch can capture the error value when the expression fails.
if (parse(text)) |value| {
use(value);
} else |err| {
report(err);
}Braces Are Required
Zig always needs braces around if bodies, even for a single line. This removes a whole class of dangling-statement bugs.
Returning Early
A common pattern is an early return inside an if. Handle the special case first, then let the rest of the function read straight down.
if (list.len == 0) return;Quick Check
You want max to hold the larger of a and b in one line. Which Zig form works?
Recap
You met if as both a statement and an expression: bool conditions, else and else if, value-returning branches, and optional or error capturing. 🎯
Frequently asked questions
Is the “if as a Statement and Expression” lesson free?
Yes — the full text of “if as a Statement and Expression” is free to read here on the web, and the Zig Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Zig Academy course, upgrade to CoddyKit PRO.
What will I learn in “if as a Statement and Expression”?
Branch and return values in one form. You practise Zig Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Zig Academy?
No prior experience is required. Zig Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “if as a Statement and Expression” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Zig Academy lesson?
Yes. Every Zig Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- if as a Statement and Expression
- while Loops and continue Expressions
- for Loops over Ranges and Items
- break, continue, and Labeled Loops