Added Ex 11-14: while loops
parent
0bb89e3e41
commit
483fb97dfc
@ -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});
|
||||||
|
}
|
@ -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});
|
||||||
|
}
|
@ -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", .{});
|
||||||
|
}
|
@ -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});
|
||||||
|
}
|
Loading…
Reference in New Issue