From 55ad7c32f2d534b1fbd438204d21738f958c51a5 Mon Sep 17 00:00:00 2001 From: Dave Gauer Date: Tue, 9 Feb 2021 18:36:57 -0500 Subject: Moved exercises to exercises because exercises --- exercises/32_unreachable.zig | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 exercises/32_unreachable.zig (limited to 'exercises/32_unreachable.zig') diff --git a/exercises/32_unreachable.zig b/exercises/32_unreachable.zig new file mode 100644 index 0000000..c81efac --- /dev/null +++ b/exercises/32_unreachable.zig @@ -0,0 +1,38 @@ +// +// Zig has an "unreachable" statement. Use it when you want to tell the +// compiler that a branch of code should never be executed and that the +// mere act of reaching it is an error. +// +// if (true) { +// ... +// } else { +// unreachable; +// } +// +// Here we've made a little virtual machine that performs mathematical +// operations on a single numeric value. It looks great but there's one +// little problem: the switch statement doesn't cover every possible +// value of a u8 number! +// +// WE know there are only three operations but Zig doesn't. Use the +// unreachable statement to make the switch complete. Or ELSE. :-) +// +const std = @import("std"); + +pub fn main() void { + const operations = [_]u8{ 1, 1, 1, 3, 2, 2 }; + + var current_value: u32 = 0; + + for (operations) |op| { + switch (op) { + 1 => { current_value += 1; }, + 2 => { current_value -= 1; }, + 3 => { current_value *= current_value; }, + } + + std.debug.print("{} ", .{current_value}); + } + + std.debug.print("\n", .{}); +} -- cgit v1.2.3-ZIG