Lesson contents

Choose between two values

Pick one value when a condition is true and the other when it is false. Here 1 means yes and 0 means no.

Simple example

Choose 1 if two is at least four.

Start2 ≥ 4
Result0
lesson.zig
const std = @import("std");

pub fn main() !void {
    const answer: i64 = if (2 >= 4) 1 else 0;
    std.debug.print("{any}\n", .{answer});
}
Output
0
Advertisements

Everyday examples

A shop offers free delivery for orders worth 20 coins or more. Does a 25-coin order qualify?

Start25 ≥ 20
Result1
lesson.zig
const std = @import("std");

pub fn main() !void {
    const free_delivery: i64 = if (25 >= 20) 1 else 0;
    std.debug.print("{any}\n", .{free_delivery});
}
Output
1
Run the example
Terminal
zig run lesson.zig
Official documentation