Added Ex 9,10 for If
parent
b9b89737fc
commit
0bb89e3e41
@ -0,0 +1,31 @@
|
||||
//
|
||||
// Now we get into the fun stuff, starting with the 'if' statement!
|
||||
//
|
||||
// if (true) {
|
||||
// // stuff
|
||||
// } else {
|
||||
// // other stuff
|
||||
// }
|
||||
//
|
||||
// Zig has the usual comparison operators such as:
|
||||
//
|
||||
// a == b a equals b
|
||||
// a < b a is less than b
|
||||
// a !=b a does not equal b
|
||||
//
|
||||
// The important thing about Zig's 'if' is that it *only* accepts
|
||||
// boolean values. It won't coerce numbers or other types of data
|
||||
// to true and false.
|
||||
//
|
||||
const std = @import("std");
|
||||
|
||||
pub fn main() void {
|
||||
const foo = 1;
|
||||
|
||||
if (foo) {
|
||||
// We want out program to print this message!
|
||||
std.debug.print("Foo is 1!\n", .{});
|
||||
} else {
|
||||
std.debug.print("Foo is not 1!\n", .{});
|
||||
}
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
//
|
||||
// If statements are also valid expressions:
|
||||
//
|
||||
// foo = if (a) 2 else 3;
|
||||
//
|
||||
// Note: you'll need to declare a variable type when assigning a value
|
||||
// from a statement like this because the compiler isn't smart enough
|
||||
// to infer the type for you.
|
||||
//
|
||||
const std = @import("std");
|
||||
|
||||
pub fn main() void {
|
||||
var discount = true;
|
||||
|
||||
// If discount is true, the price should be $17, otherwise $20:
|
||||
var price = if ???;
|
||||
|
||||
std.debug.print("With the discount, the price is ${}.\n", .{price});
|
||||
}
|
||||
|
Loading…
Reference in New Issue