my ziglings
0

Configure Feed

Select the types of activity you want to include in your feed.

Updating wording in 'for' exercises

This is in preparation for another dive into 'for'
in an upcoming Exercise 100.

Also reformatted 095 for 65 columns and some wording.

Dave Gauer (Apr 30, 2023, 4:23 PM EDT) e9e6be4e 6b48914d

+57 -38
+5 -1
exercises/015_for.zig
··· 23 23 24 24 std.debug.print("The End.\n", .{}); 25 25 } 26 - // Note that "for" loops also work on things called "slices" 26 + // Note that 'for' loops also work on things called "slices" 27 27 // which we'll see later. 28 + // 29 + // Also note that 'for' loops have recently become more flexible 30 + // and powerful (two years after this exercise was written). 31 + // More about that in a moment.
+6
exercises/016_for2.zig
··· 35 35 36 36 std.debug.print("The value of bits '1101': {}.\n", .{value}); 37 37 } 38 + // 39 + // As mentioned in the previous exercise, 'for' loops have gained 40 + // additional flexibility since these early exercises were 41 + // written. As we'll see in later exercises, the above syntax for 42 + // capturing the index is part of a more general ability. hang in 43 + // there!
+46 -37
exercises/095_for_loops.zig
··· 1 1 // 2 - // The Zig language is in rapid development and continuously improves 3 - // the language constructs steadily. 2 + // The Zig language is in rapid development and continuously 3 + // improves the language constructs. Ziglings evolves with it. 4 4 // 5 - // Since version 0.11, the "for-loops" widely used in other languages 6 - // such as C, e.g. "for (int i = 0; i < 10..." can now also be formed 7 - // similarly in Zig, which previously required a "while" construct. 8 - // Similar in this case actually means better, just as Zig generally 9 - // tries to make everything simple and "better". 5 + // Until version 0.11, Zig's 'for' loops did not directly 6 + // replicate the functionality of the C-style: "for(a;b;c)" 7 + // which are so well suited for iterating over a numeric 8 + // sequence. 10 9 // 11 - // These new "for-loops" look like the following in Zig: 10 + // Instead, 'while' loops with counters clumsily stood in their 11 + // place: 12 12 // 13 - // for (0..10) |idx| { 14 - // // In this case 'idx' takes all values from 0 to 9. 15 - // } 13 + // var i: usize = 0; 14 + // while (i < 10) : (i += 1) { 15 + // // Here variable 'i' will have each value 0 to 9. 16 + // } 16 17 // 17 - // This is really simple and can replace the previous, somewhat bulky: 18 + // But here we are in the glorious future and Zig's 'for' loops 19 + // can now take this form: 18 20 // 19 - // var idx: usize = 0; 20 - // while (idx < 10) : (idx += 1) { 21 - // // Again, idx takes all values from 0 to 9. 22 - // } 21 + // for (0..10) |i| { 22 + // // Here variable 'i' will have each value 0 to 9. 23 + // } 23 24 // 24 - // This would also simplify exercise 13, for example. 25 - // The best way to try this out is to use this exercise, which in the 26 - // original looks like this: 25 + // The key to understanding this example is to know that '0..9' 26 + // uses the new range syntax: 27 27 // 28 - // ... 29 - // var n: u32 = 1; 28 + // 0..10 is a range from 0 to 9 29 + // 1..4 is a range from 1 to 3 30 30 // 31 - // // I want to print every number between 1 and 20 that is NOT 32 - // // divisible by 3 or 5. 33 - // while (n <= 20) : (n += 1) { 34 - // // The '%' symbol is the "modulo" operator and it 35 - // // returns the remainder after division. 36 - // if (n % 3 == 0) continue; 37 - // if (n % 5 == 0) continue; 38 - // std.debug.print("{} ", .{n}); 39 - // } 40 - // ... 31 + // At the moment, ranges are only supported in 'for' loops. 32 + // 33 + // Perhaps you recall Exercise 13? We were printing a numeric 34 + // sequence like so: 35 + // 36 + // var n: u32 = 1; 37 + // 38 + // // I want to print every number between 1 and 20 that is NOT 39 + // // divisible by 3 or 5. 40 + // while (n <= 20) : (n += 1) { 41 + // // The '%' symbol is the "modulo" operator and it 42 + // // returns the remainder after division. 43 + // if (n % 3 == 0) continue; 44 + // if (n % 5 == 0) continue; 45 + // std.debug.print("{} ", .{n}); 46 + // } 47 + // 48 + // Let's try out the new form of 'for' to re-implement that 49 + // exercise: 41 50 // 42 51 const std = @import("std"); 43 52 44 - // And now with the new "for-loop". 45 53 pub fn main() void { 46 54 47 55 // I want to print every number between 1 and 20 that is NOT ··· 57 65 58 66 std.debug.print("\n", .{}); 59 67 } 60 - 61 - // Is actually a little easier. The interesting thing here is that the other 62 - // previous 'while' exercises (11,12, 14) cannot be simplified by this 63 - // new "for-loop". Therefore it is good to be able to use both variations 64 - // accordingly. 68 + // 69 + // That's a bit nicer, right? 70 + // 71 + // Of course, both 'while' and 'for' have different advantages. 72 + // Exercises 11, 12, and 14 would NOT be simplified by switching 73 + // a 'while' for a 'for'.