12_while2.zig 779 B

123456789101112131415161718192021222324252627282930313233
  1. //
  2. // Zig 'while' statements can have an optional 'continue expression'
  3. // which runs every time the while loop continues (either at the
  4. // end of the loop or when an explicit 'continue' is invoked (we'll
  5. // try those out next):
  6. //
  7. // while (condition) : (continue expression)
  8. // ...
  9. // }
  10. //
  11. // Example:
  12. //
  13. // var foo = 2;
  14. // while (foo<10) : (foo+=2)
  15. // // Do something with even numbers less than 10...
  16. // }
  17. //
  18. // See if you can re-write the last exercise using a continue
  19. // expression:
  20. //
  21. const std = @import("std");
  22. pub fn main() void {
  23. var n: u32 = 2;
  24. while (n < 1000) : ??? {
  25. // Print the current number
  26. std.debug.print("{} ", .{n});
  27. }
  28. // Make this print n=1024
  29. std.debug.print("n={}\n", .{n});
  30. }