Back to course

Switch Statements

Full Course: Zig Programming From Zero to Hero

Switch Statements

In Zig, switch is powerful and exhaustive. This means you must cover all possible cases, or use an else branch.

zig const os = 1; switch (os) { 1 => std.debug.print("Linux\n", .{}), 2 => std.debug.print("Windows\n", .{}), else => std.debug.print("Other\n", .{}), }

You can also use ranges and multiple values:

zig switch (score) { 0...50 => "Fail", 51...100 => "Pass", else => "Invalid", }