Inserted ex. 32 unreachable, added quiz4.
parent
cd80aeb190
commit
738a9f6cda
@ -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", .{});
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
//
|
||||
// Quiz time. See if you can make this program work!
|
||||
//
|
||||
// Solve this any way you like, just be sure the output is:
|
||||
//
|
||||
// my_num=42
|
||||
//
|
||||
const std = @import("std");
|
||||
|
||||
const NumError = error{ IllegalNumber };
|
||||
|
||||
pub fn main() void {
|
||||
const stdout = std.io.getStdOut().writer();
|
||||
|
||||
const my_num: u32 = getNumber();
|
||||
|
||||
try stdout.print("my_num={}\n", .{my_num});
|
||||
}
|
||||
|
||||
// Just don't modify this function. It's "perfect" the way it is. :-)
|
||||
fn getNumber() NumError!u32 {
|
||||
if( false ) return NumError.IllegalNumber;
|
||||
return 42;
|
||||
}
|
Loading…
Reference in New Issue