Added Ex 11-14: while loops

This commit is contained in:
Dave Gauer 2021-01-10 11:46:42 -05:00
parent 0bb89e3e41
commit 483fb97dfc
6 changed files with 129 additions and 1 deletions

33
11_while.zig Normal file
View file

@ -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});
}

33
12_while2.zig Normal file
View file

@ -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});
}

30
13_while3.zig Normal file
View file

@ -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", .{});
}

24
14_while4.zig Normal file
View file

@ -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});
}

View file

@ -63,7 +63,7 @@ Planned exercises:
* [x] Arrays * [x] Arrays
* [x] Strings * [x] Strings
* [x] If * [x] If
* [ ] While * [x] While
* [ ] For * [ ] For
* [ ] Functions * [ ] Functions
* [ ] Defer * [ ] Defer

View file

@ -59,6 +59,10 @@ function check_it {
printf "${fmt_yay}** PASSED **${fmt_off}\n" printf "${fmt_yay}** PASSED **${fmt_off}\n"
else else
printf "${fmt_err}It seems to compile, but I wanted to see '$correct_output'.${fmt_off}\n" 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 echo
exit 1 exit 1
fi 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 08_quiz.zig "Program in Zig" "See if you can fix the program!"
check_it 09_if.zig "Foo is 1!" check_it 09_if.zig "Foo is 1!"
check_it 10_if2.zig "price is \$17" 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
echo " __ __ _ " echo " __ __ _ "