mirror of
https://codeberg.org/ziglings/exercises.git
synced 2024-12-25 09:10:26 +00:00
improve explanantions in labeled switch
This commit is contained in:
parent
d9ce18631d
commit
ed1a20d51b
1 changed files with 49 additions and 10 deletions
|
@ -1,15 +1,54 @@
|
||||||
//
|
//
|
||||||
// A labeled switch in zig allows the usage of continue and break
|
// You've heard of while loops in exercises 011,012,013 and 014
|
||||||
// just like loops, these allow you to create very concise
|
// You've also heard of switch expressions in exercises 030 and 31.
|
||||||
// Finite State Automata to represent state transitions
|
// You've also seen how labels can be used in exercise 063.
|
||||||
//
|
//
|
||||||
// foo: switch (state) {
|
// By combining while loops and switch statements with continue and break statements
|
||||||
// 1 => continue :foo 2,
|
// one can create very concise State Machines.
|
||||||
// 2 => continue :foo 3,
|
//
|
||||||
// 3 => break :foo,
|
// One such example would be:
|
||||||
// 4 => {},
|
//
|
||||||
// ...
|
// pub fn main() void {
|
||||||
// }
|
// var op: u8 = 1;
|
||||||
|
// while (true) {
|
||||||
|
// switch (op) {
|
||||||
|
// 1 => { op = 2; continue; },
|
||||||
|
// 2 => { op = 3; continue; },
|
||||||
|
// 3 => return,
|
||||||
|
// 4 => {},
|
||||||
|
// }
|
||||||
|
// break;
|
||||||
|
// }
|
||||||
|
// std.debug.print("This statement cannot be reached");
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// By combining all we've learned so far, we can now proceed with a labeled switch
|
||||||
|
//
|
||||||
|
// A labeled switch is some extra syntatic sugar, which comes with all sorts of
|
||||||
|
// candy (performance benefits). Don't believe me? Directly to source https://github.com/ziglang/zig/pull/21367
|
||||||
|
//
|
||||||
|
// Here is the previous excerpt implemented as a labeled switch instead:
|
||||||
|
//
|
||||||
|
// pub fn main() void {
|
||||||
|
// foo: switch (@as(u8, 1)) {
|
||||||
|
// 1 => continue :foo 2,
|
||||||
|
// 2 => continue :foo 3,
|
||||||
|
// 3 => return,
|
||||||
|
// 4 => {},
|
||||||
|
// }
|
||||||
|
// std.debug.print("This statement cannot be reached");
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// The flow of execution on this second case is:
|
||||||
|
// 1. The switch starts with value '1';
|
||||||
|
// 2. The switch evaluates to case '1' which in turn uses the continue statement
|
||||||
|
// to re-evaluate the labeled switch again, now providing the value '2';
|
||||||
|
// 3. In the case '2' we repeat the same pattern as case '1'
|
||||||
|
// but instead the value to be evaluated is now '3';
|
||||||
|
// 4. Finally we get to case '3', where we return from the function as a whole.
|
||||||
|
//
|
||||||
|
// Since step 4 or a break stament do not exist in this switch, the debug statement is
|
||||||
|
// never executed
|
||||||
//
|
//
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue