summaryrefslogtreecommitdiffstatshomepage
path: root/exercises/032_unreachable.zig
diff options
context:
space:
mode:
authorDave Gauer <dave@ratfactor.com>2021-03-12 18:59:46 -0500
committerDave Gauer <dave@ratfactor.com>2021-03-12 18:59:46 -0500
commit0956f1839fcaaa273353148da9e157a8f9690d2f (patch)
treed6c90700131d5b28e898881f13e2a05612e4703f /exercises/032_unreachable.zig
parent93eefe0f250bb76bfdd8e6bb784b6a9586517000 (diff)
"999 is enough for anybody" triple-zero padding (#18)
When I hit 999 exercises, I will finally have reached the ultimate state of soteriological release and no more exercises will be needed. The cycle will be complete. All that will be left is perfect quietude, freedom, and highest happiness.
Diffstat (limited to 'exercises/032_unreachable.zig')
-rw-r--r--exercises/032_unreachable.zig44
1 files changed, 44 insertions, 0 deletions
diff --git a/exercises/032_unreachable.zig b/exercises/032_unreachable.zig
new file mode 100644
index 0000000..ffc35a4
--- /dev/null
+++ b/exercises/032_unreachable.zig
@@ -0,0 +1,44 @@
+//
+// 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", .{});
+}