012_while2.zig 939 B

1234567891011121314151617181920212223242526272829303132333435
  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. // Please set the continue expression so that we get the desired
  25. // results in the print statement below.
  26. while (n < 1000) : ??? {
  27. // Print the current number
  28. std.debug.print("{} ", .{n});
  29. }
  30. // As in the last exercise, we want this to result in "n=1024"
  31. std.debug.print("n={}\n", .{n});
  32. }