diff options
-rw-r--r-- | 11_while.zig | 33 | ||||
-rw-r--r-- | 12_while2.zig | 33 | ||||
-rw-r--r-- | 13_while3.zig | 30 | ||||
-rw-r--r-- | 14_while4.zig | 24 | ||||
-rw-r--r-- | README.md | 2 | ||||
-rwxr-xr-x | ziglings | 8 |
6 files changed, 129 insertions, 1 deletions
diff --git a/11_while.zig b/11_while.zig new file mode 100644 index 0000000..820cf56 --- /dev/null +++ b/11_while.zig @@ -0,0 +1,33 @@ +// +// Zig 'while' statements create a loop that runs while the +// condition is true: +// +// while (condition) { +// condition = false; +// } +// +// Remember that the condition must be a boolean value and +// that we can get a boolean value from conditional operators +// such as: +// +// a == b a equals b +// a < b a is less than b +// a > b a is greater than b +// a !=b a does not equal b +// +const std = @import("std"); + +pub fn main() void { + var n: u32 = 2; + + while ( ??? ){ + // Print the current number + std.debug.print("{} ", .{n}); + + // Set n to n multiplied by 2 + n *= 2; + } + + // Make this print n=1024 + std.debug.print("n={}\n", .{n}); +} diff --git a/12_while2.zig b/12_while2.zig new file mode 100644 index 0000000..dba1a26 --- /dev/null +++ b/12_while2.zig @@ -0,0 +1,33 @@ +// +// Zig 'while' statements can have an optional 'continue expression' +// which runs every time the while loop continues (either at the +// end of the loop or when an explicit 'continue' is invoked (we'll +// try those out next): +// +// while (condition) : (continue expression) +// ... +// } +// +// Example: +// +// var foo = 2; +// while (foo<10) : (foo+=2) +// // Do something with even numbers less than 10... +// } +// +// See if you can re-write the last exercise using a continue +// expression: +// +const std = @import("std"); + +pub fn main() void { + var n: u32 = 2; + + while (n < 1000) : ??? { + // Print the current number + std.debug.print("{} ", .{n}); + } + + // Make this print n=1024 + std.debug.print("n={}\n", .{n}); +} diff --git a/13_while3.zig b/13_while3.zig new file mode 100644 index 0000000..adb74ef --- /dev/null +++ b/13_while3.zig @@ -0,0 +1,30 @@ +// +// The last two exercises were functionally identical. Continue +// expressions really show their utility when used with 'continue' +// statements! +// +// Example: +// +// while (condition) : (continue expression){ +// if(other condition) continue; +// ... +// } +// +// The continue expression executes even when 'other condition' +// is true and the loop is restarted by the 'continue' statement. +// +const std = @import("std"); + +pub fn main() void { + 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) { + if(n % 3 == 0) ???; + if(n % 5 == 0) ???; + std.debug.print("{} ", .{n}); + } + + std.debug.print("\n", .{}); +} diff --git a/14_while4.zig b/14_while4.zig new file mode 100644 index 0000000..e686f88 --- /dev/null +++ b/14_while4.zig @@ -0,0 +1,24 @@ +// +// Continue expressions do NOT execute when a while loop stops +// because of a 'break' statement. +// +// Example: +// +// while (condition) : (continue expression){ +// if(other condition) break; +// ... +// } +// +const std = @import("std"); + +pub fn main() void { + var n: u32 = 1; + + // Oh dear! This while loop will go forever!? + while (true) : (n+=1) { + if(???) ???; + } + + // Result: we want n=4 + std.debug.print("n={}\n", .{n}); +} @@ -63,7 +63,7 @@ Planned exercises: * [x] Arrays * [x] Strings * [x] If -* [ ] While +* [x] While * [ ] For * [ ] Functions * [ ] Defer @@ -59,6 +59,10 @@ function check_it { printf "${fmt_yay}** PASSED **${fmt_off}\n" else printf "${fmt_err}It seems to compile, but I wanted to see '$correct_output'.${fmt_off}\n" + if [[ ! -z "$hint" ]] + then + echo "$hint" + fi echo exit 1 fi @@ -75,6 +79,10 @@ check_it 07_strings2.zig "Ziggy" "Please fix the lyrics!" check_it 08_quiz.zig "Program in Zig" "See if you can fix the program!" check_it 09_if.zig "Foo is 1!" check_it 10_if2.zig "price is \$17" +check_it 11_while.zig "n=1024" "You probably want a 'less than' condition." +check_it 12_while2.zig "n=1024" "It might help to look back at the previous exercise." +check_it 13_while3.zig "1 2 4 7 8 11 13 14 16 17 19" +check_it 14_while4.zig "n=4" echo echo " __ __ _ " |