aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
-rw-r--r--09_if.zig31
-rw-r--r--10_if2.zig20
-rw-r--r--README.md2
-rwxr-xr-xziglings2
4 files changed, 54 insertions, 1 deletions
diff --git a/09_if.zig b/09_if.zig
new file mode 100644
index 0000000..3309cbf
--- /dev/null
+++ b/09_if.zig
@@ -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", .{});
+ }
+}
diff --git a/10_if2.zig b/10_if2.zig
new file mode 100644
index 0000000..5e78d54
--- /dev/null
+++ b/10_if2.zig
@@ -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});
+}
+
diff --git a/README.md b/README.md
index 07fe634..f43a95a 100644
--- a/README.md
+++ b/README.md
@@ -62,7 +62,7 @@ Planned exercises:
* [x] Assignment
* [x] Arrays
* [x] Strings
-* [ ] If
+* [x] If
* [ ] While
* [ ] For
* [ ] Functions
diff --git a/ziglings b/ziglings
index 7825cf6..2e180f0 100755
--- a/ziglings
+++ b/ziglings
@@ -73,6 +73,8 @@ check_it 05_arrays2.zig "LEET: 1337, Bits: 100110011001" "Fill in the two arrays
check_it 06_strings.zig "d=d ha ha ha Major Tom" "Each '???' needs something filled in."
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"
echo
echo " __ __ _ "