031_switch2.zig 961 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. //
  2. // What's really nice is that you can use a switch statement as an
  3. // expression to return a value.
  4. //
  5. // const a = switch (x) {
  6. // 1 => 9,
  7. // 2 => 16,
  8. // 3 => 7,
  9. // ...
  10. // }
  11. //
  12. const std = @import("std");
  13. pub fn main() void{
  14. const lang_chars = [_]u8{ 26, 9, 7, 42 };
  15. for(lang_chars) |c|{
  16. const real_char: u8 = switch(c){
  17. 1 => 'A',
  18. 2 => 'B',
  19. 3 => 'C',
  20. 4 => 'D',
  21. 5 => 'E',
  22. 6 => 'F',
  23. 7 => 'G',
  24. 8 => 'H',
  25. 9 => 'I',
  26. 10 => 'J',
  27. // ...
  28. 25 => 'Y',
  29. 26 => 'Z',
  30. // As in the last exercise, please add the 'else' clause
  31. // and this time, have it return an exclamation mark '!'.
  32. else => '!',
  33. };
  34. std.debug.print("{c}", .{real_char});
  35. // Note: "{c}" forces print() to display the value as a character.
  36. // Can you guess what happens if you remove the "c"? Try it!
  37. }
  38. std.debug.print("\n", .{});
  39. }