014_while4.zig 588 B

123456789101112131415161718192021222324252627
  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(n == 4){ break; }
  20. }
  21. // Result: we want n=4
  22. std.debug.print("n={}\n", .{n});
  23. }