···23232424 std.debug.print("The End.\n", .{});
2525}
2626-// Note that "for" loops also work on things called "slices"
2626+// Note that 'for' loops also work on things called "slices"
2727// which we'll see later.
2828+//
2929+// Also note that 'for' loops have recently become more flexible
3030+// and powerful (two years after this exercise was written).
3131+// More about that in a moment.
+6
exercises/016_for2.zig
···35353636 std.debug.print("The value of bits '1101': {}.\n", .{value});
3737}
3838+//
3939+// As mentioned in the previous exercise, 'for' loops have gained
4040+// additional flexibility since these early exercises were
4141+// written. As we'll see in later exercises, the above syntax for
4242+// capturing the index is part of a more general ability. hang in
4343+// there!
+46-37
exercises/095_for_loops.zig
···11//
22-// The Zig language is in rapid development and continuously improves
33-// the language constructs steadily.
22+// The Zig language is in rapid development and continuously
33+// improves the language constructs. Ziglings evolves with it.
44//
55-// Since version 0.11, the "for-loops" widely used in other languages
66-// such as C, e.g. "for (int i = 0; i < 10..." can now also be formed
77-// similarly in Zig, which previously required a "while" construct.
88-// Similar in this case actually means better, just as Zig generally
99-// tries to make everything simple and "better".
55+// Until version 0.11, Zig's 'for' loops did not directly
66+// replicate the functionality of the C-style: "for(a;b;c)"
77+// which are so well suited for iterating over a numeric
88+// sequence.
109//
1111-// These new "for-loops" look like the following in Zig:
1010+// Instead, 'while' loops with counters clumsily stood in their
1111+// place:
1212//
1313-// for (0..10) |idx| {
1414-// // In this case 'idx' takes all values from 0 to 9.
1515-// }
1313+// var i: usize = 0;
1414+// while (i < 10) : (i += 1) {
1515+// // Here variable 'i' will have each value 0 to 9.
1616+// }
1617//
1717-// This is really simple and can replace the previous, somewhat bulky:
1818+// But here we are in the glorious future and Zig's 'for' loops
1919+// can now take this form:
1820//
1919-// var idx: usize = 0;
2020-// while (idx < 10) : (idx += 1) {
2121-// // Again, idx takes all values from 0 to 9.
2222-// }
2121+// for (0..10) |i| {
2222+// // Here variable 'i' will have each value 0 to 9.
2323+// }
2324//
2424-// This would also simplify exercise 13, for example.
2525-// The best way to try this out is to use this exercise, which in the
2626-// original looks like this:
2525+// The key to understanding this example is to know that '0..9'
2626+// uses the new range syntax:
2727//
2828-// ...
2929-// var n: u32 = 1;
2828+// 0..10 is a range from 0 to 9
2929+// 1..4 is a range from 1 to 3
3030//
3131-// // I want to print every number between 1 and 20 that is NOT
3232-// // divisible by 3 or 5.
3333-// while (n <= 20) : (n += 1) {
3434-// // The '%' symbol is the "modulo" operator and it
3535-// // returns the remainder after division.
3636-// if (n % 3 == 0) continue;
3737-// if (n % 5 == 0) continue;
3838-// std.debug.print("{} ", .{n});
3939-// }
4040-// ...
3131+// At the moment, ranges are only supported in 'for' loops.
3232+//
3333+// Perhaps you recall Exercise 13? We were printing a numeric
3434+// sequence like so:
3535+//
3636+// var n: u32 = 1;
3737+//
3838+// // I want to print every number between 1 and 20 that is NOT
3939+// // divisible by 3 or 5.
4040+// while (n <= 20) : (n += 1) {
4141+// // The '%' symbol is the "modulo" operator and it
4242+// // returns the remainder after division.
4343+// if (n % 3 == 0) continue;
4444+// if (n % 5 == 0) continue;
4545+// std.debug.print("{} ", .{n});
4646+// }
4747+//
4848+// Let's try out the new form of 'for' to re-implement that
4949+// exercise:
4150//
4251const std = @import("std");
43524444-// And now with the new "for-loop".
4553pub fn main() void {
46544755 // I want to print every number between 1 and 20 that is NOT
···57655866 std.debug.print("\n", .{});
5967}
6060-6161-// Is actually a little easier. The interesting thing here is that the other
6262-// previous 'while' exercises (11,12, 14) cannot be simplified by this
6363-// new "for-loop". Therefore it is good to be able to use both variations
6464-// accordingly.
6868+//
6969+// That's a bit nicer, right?
7070+//
7171+// Of course, both 'while' and 'for' have different advantages.
7272+// Exercises 11, 12, and 14 would NOT be simplified by switching
7373+// a 'while' for a 'for'.