014_while4.zig 597 B

1234567891011121314151617181920212223242526
  1. //
  2. // You can force a loop to exit immediately with a "break" statement:
  3. //
  4. // while (condition) : (continue expression) {
  5. //
  6. // if (other condition) break;
  7. //
  8. // }
  9. //
  10. // Continue expressions do NOT execute when a while loop stops
  11. // because of a break!
  12. //
  13. const std = @import("std");
  14. pub fn main() void {
  15. var n: u32 = 1;
  16. // Oh dear! This while loop will go forever?!
  17. // Please fix this so the print statement below gives the desired output.
  18. while (true) : (n += 1) {
  19. if (???) ???;
  20. }
  21. // Result: we want n=4
  22. std.debug.print("n={}\n", .{n});
  23. }