Zig Booleans
A boolean has exactly two values: true and false. Booleans drive every decision in a program — whether an if branch runs, whether a loop continues. Zig's type for them is bool.
Declaring booleans
const std = @import("std");
pub fn main() void {
const is_open = true;
const has_stock: bool = false;
std.debug.print("open: {}, stock: {}\n", .{ is_open, has_stock });
std.debug.print("size of bool: {d} byte\n", .{@sizeOf(bool)});
}open: true, stock: false
size of bool: 1 byte
Comparisons produce booleans
| Operator | Meaning |
|---|---|
== |
equal |
!= |
not equal |
<, <= |
less than, less or equal |
>, >= |
greater than, greater or equal |
const std = @import("std");
pub fn main() void {
const age: u8 = 17;
const adult = age >= 18;
const teen = age >= 13 and age <= 19;
std.debug.print("adult: {}\n", .{adult});
std.debug.print("teen: {}\n", .{teen});
std.debug.print("exactly 17: {}\n", .{age == 17});
}adult: false
teen: true
exactly 17: true
Logical operators are words
Zig spells logical operators as keywords instead of symbols. This makes their special behaviour obvious: they short-circuit, meaning the right side is only evaluated when needed.
| Zig | C equivalent | True when |
|---|---|---|
a and b |
a && b |
both are true |
a or b |
a || b |
at least one is true |
!a |
!a |
a is false |
const std = @import("std");
fn expensiveCheck() bool {
std.debug.print(" (expensive check ran)\n", .{});
return true;
}
pub fn main() void {
const cached = true;
std.debug.print("using or:\n", .{});
const ok1 = cached or expensiveCheck();
std.debug.print("using and:\n", .{});
const ok2 = cached and expensiveCheck();
std.debug.print("{} {} {}\n", .{ ok1, ok2, !ok1 });
}using or:
using and:
(expensive check ran)
true true false
With or, the left side was already true, so expensiveCheck never ran. With and, the result depended on the right side, so it had to run.
No truthiness
Many languages treat 0, empty strings or null as "false". Zig does not. A condition must be a real bool:
const std = @import("std");
pub fn main() void {
const items: u32 = 3;
if (items) {
std.debug.print("have items\n", .{});
}
}./example.zig:5:9: error: expected type 'bool', found 'u32'
if (items) {
^~~~~Write the comparison you mean: if (items > 0) or if (items != 0).
Converting booleans to numbers
Occasionally you need a boolean as a number, for example to count how many conditions hold. @intFromBool turns true into 1 and false into 0:
const std = @import("std");
pub fn main() void {
const answers = [_]bool{ true, false, true, true };
var correct: u32 = 0;
for (answers) |a| {
correct += @intFromBool(a);
}
std.debug.print("{d} of {d} correct\n", .{ correct, answers.len });
}3 of 4 correct
Tip: Name booleans as yes/no questions —
is_ready,has_error,can_edit— so conditions read like sentences:if (can_edit and !is_locked).
Test yourself
How is logical AND written in Zig?
- &&
- and
- &
- AND
What happens with if (count) where count is a u32?
- Runs if count is not 0
- Runs if count is 0
- Compile error: expected type 'bool'
- Runtime panic
In a or f(), when is f() called?
- Always
- Only when
ais false - Only when
ais true - Never