diff options
author | Dave Gauer <dave@ratfactor.com> | 2021-01-31 17:48:34 -0500 |
---|---|---|
committer | Dave Gauer <dave@ratfactor.com> | 2021-01-31 17:48:34 -0500 |
commit | c70fa5f58f5ca2dc010f00caee19027069a09131 (patch) | |
tree | 9f7d352065d282c15a75aa4c4168216e69a762f8 /30_switch.zig | |
parent | 2de8a8c54d7090dd063bed8b6b283c2fcb452e43 (diff) | |
download | ziglings-c70fa5f58f5ca2dc010f00caee19027069a09131.tar.gz ziglings-c70fa5f58f5ca2dc010f00caee19027069a09131.tar.bz2 ziglings-c70fa5f58f5ca2dc010f00caee19027069a09131.tar.xz ziglings-c70fa5f58f5ca2dc010f00caee19027069a09131.zip |
Adding exs 27-32
Diffstat (limited to '30_switch.zig')
-rw-r--r-- | 30_switch.zig | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/30_switch.zig b/30_switch.zig new file mode 100644 index 0000000..b10ad14 --- /dev/null +++ b/30_switch.zig @@ -0,0 +1,55 @@ +// +// The "switch" statement lets you match the possible values of an +// expression and perform a different action for each. +// +// This switch: +// +// switch (players) { +// 1 => startOnePlayerGame(), +// 2 => startTwoPlayerGame(), +// else => { +// alert(); +// return GameError.TooManyPlayers; +// } +// } +// +// Is equivalent to this if/else: +// +// if (players == 1) startOnePlayerGame(); +// else if (players == 2) startTwoPlayerGame(); +// else { +// alert(); +// return GameError.TooManyPlayers; +// } +// +// +// +const std = @import("std"); + +pub fn main() void { + const lang_chars = [_]u8{ 26, 9, 7, 42 }; + + for (lang_chars) |c| { + switch (c) { + 1 => std.debug.print("A", .{}), + 2 => std.debug.print("B", .{}), + 3 => std.debug.print("C", .{}), + 4 => std.debug.print("D", .{}), + 5 => std.debug.print("E", .{}), + 6 => std.debug.print("F", .{}), + 7 => std.debug.print("G", .{}), + 8 => std.debug.print("H", .{}), + 9 => std.debug.print("I", .{}), + 10 => std.debug.print("J", .{}), + // ... we don't need everything in between ... + 25 => std.debug.print("Y", .{}), + 26 => std.debug.print("Z", .{}), + // Switch statements must be "exhaustive" (there must be a + // match for every possible value). Please add an "else" + // to this switch to print a question mark "?" when c is + // not one of the existing matches. + } + } + + std.debug.print("\n", .{}); +} |