if as a Statement and Expression
Branch and return values in one form.
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.
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