aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/exercises
diff options
context:
space:
mode:
authorChris Boesch <chrboesch@noreply.codeberg.org>2023-03-04 16:07:53 +0100
committerChris Boesch <chrboesch@noreply.codeberg.org>2023-03-04 16:07:53 +0100
commit101151f3f130f845a534caa00537846b70150ac7 (patch)
tree4262da1440019beb86e695497037db3ddeac1e0d /exercises
parent582f3b1b09cdbca7b27fc657998a4658439e45d7 (diff)
exercise for new for-loops
Diffstat (limited to 'exercises')
-rw-r--r--exercises/095_for_loops.zig64
1 files changed, 64 insertions, 0 deletions
diff --git a/exercises/095_for_loops.zig b/exercises/095_for_loops.zig
new file mode 100644
index 0000000..b437946
--- /dev/null
+++ b/exercises/095_for_loops.zig
@@ -0,0 +1,64 @@
+//
+// The Zig language is in rapid development and continuously improves
+// the language constructs steadily.
+//
+// Since version 0.11, the "for-loops" widely used in other languages
+// such as C, e.g. "for (int i = 0; i < 10..." can now also be formed
+// similarly in Zig, which previously required a "while" construct.
+// Similar in this case actually means better, just as Zig generally
+// tries to make everything simple and "better".
+//
+// These new "for-loops" look like the following in Zig:
+//
+// for (0..10) |idx| {
+// // In this case 'idx' takes all values from 0 to 9.
+// }
+//
+// This is really simple and can replace the previous, somewhat bulky:
+//
+// var idx: usize = 0;
+// while (idx < 10) : (idx += 1) {
+// // Again, idx takes all values from 0 to 9.
+// }
+//
+// This would also simplify exercise 13, for example.
+// The best way to try this out is to use this exercise, which in the
+// original looks like this:
+//
+// ...
+// var n: u32 = 1;
+//
+// // I want to print every number between 1 and 20 that is NOT
+// // divisible by 3 or 5.
+// while (n <= 20) : (n += 1) {
+// // The '%' symbol is the "modulo" operator and it
+// // returns the remainder after division.
+// if (n % 3 == 0) continue;
+// if (n % 5 == 0) continue;
+// std.debug.print("{} ", .{n});
+// }
+// ...
+//
+const std = @import("std");
+
+// And now with the new "for-loop".
+pub fn main() void {
+
+ // I want to print every number between 1 and 20 that is NOT
+ // divisible by 3 or 5.
+ for (???) |n| {
+
+ // The '%' symbol is the "modulo" operator and it
+ // returns the remainder after division.
+ if (n % 3 == 0) continue;
+ if (n % 5 == 0) continue;
+ std.debug.print("{} ", .{n});
+ }
+
+ std.debug.print("\n", .{});
+}
+
+// Is actually a little easier. The interesting thing here is that the other
+// previous 'while' exercises (11,12, 14) cannot be simplified by this
+// new "for-loop". Therefore it is good to be able to use both variations
+// accordingly.