aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDave Gauer <dave@ratfactor.com>2021-01-18 19:21:18 -0500
committerDave Gauer <dave@ratfactor.com>2021-01-18 19:21:18 -0500
commit2bda44bc586ee16dd3fe0bce1ead9372a83a71f3 (patch)
tree3d0291cd8e3087556094f159243c5ce0fca0cb77
parent483fb97dfccca833457f55798149b68942be6deb (diff)
downloadziglings-2bda44bc586ee16dd3fe0bce1ead9372a83a71f3.tar.gz
ziglings-2bda44bc586ee16dd3fe0bce1ead9372a83a71f3.tar.bz2
ziglings-2bda44bc586ee16dd3fe0bce1ead9372a83a71f3.tar.xz
ziglings-2bda44bc586ee16dd3fe0bce1ead9372a83a71f3.zip
Added ex 15,16 for loops
-rw-r--r--15_for.zig24
-rw-r--r--16_for2.zig27
-rw-r--r--README.md2
-rwxr-xr-xziglings3
4 files changed, 55 insertions, 1 deletions
diff --git a/15_for.zig b/15_for.zig
new file mode 100644
index 0000000..51ab67f
--- /dev/null
+++ b/15_for.zig
@@ -0,0 +1,24 @@
+//
+// Behold the 'for' loop! It lets you execute code for each
+// member of an array (and things called 'slices' which we'll
+// get to in a bit).
+//
+// for (items) |item| {
+// // Do something with item
+// }
+//
+const std = @import("std");
+
+pub fn main() void {
+ const story = [_]u8{ 'h', 'h', 's', 'n', 'h' };
+
+ std.debug.print("A Dramatic Story: ", .{});
+
+ for (???) |???| {
+ if(scene == 'h') std.debug.print(":-) ", .{});
+ if(scene == 's') std.debug.print(":-( ", .{});
+ if(scene == 'n') std.debug.print(":-| ", .{});
+ }
+
+ std.debug.print("The End.\n", .{});
+}
diff --git a/16_for2.zig b/16_for2.zig
new file mode 100644
index 0000000..914d047
--- /dev/null
+++ b/16_for2.zig
@@ -0,0 +1,27 @@
+//
+// For loops also let you store the "index" of the iteration - a
+// number starting with 0 that counts up with each iteration:
+//
+// for (items) |item, index| {
+// // Do something with item and index
+// }
+//
+const std = @import("std");
+
+pub fn main() void {
+ // Let's store the bits of binary number 1101 in
+ // 'little-endian' order (least significant byte first):
+ const bits = [_]u8{ 1, 0, 1, 1 };
+ var value: u32 = 0;
+
+ // Now we'll convert the binary bits to a number value by adding
+ // the value of the place as a power of two for each bit. See if
+ // you can figure out the missing piece:
+ //
+ for (bits) |bit, i| {
+ var place_value = std.math.pow(u32, 2, @intCast(u32, i));
+ value += place_value * bit;
+ }
+
+ std.debug.print("The value of bits '1101': {}.\n", .{value});
+}
diff --git a/README.md b/README.md
index ddda73b..4f87789 100644
--- a/README.md
+++ b/README.md
@@ -64,7 +64,7 @@ Planned exercises:
* [x] Strings
* [x] If
* [x] While
-* [ ] For
+* [x] For
* [ ] Functions
* [ ] Defer
* [ ] Errors
diff --git a/ziglings b/ziglings
index 1feac45..6c2c62c 100755
--- a/ziglings
+++ b/ziglings
@@ -83,6 +83,8 @@ 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"
+check_it 15_for.zig "A Dramatic Story: :-) :-) :-( :-| :-) The End."
+check_it 16_for2.zig "13"
echo
echo " __ __ _ "
@@ -93,4 +95,5 @@ echo " |_|\__,_|\__, (_) "
echo " |___/ "
echo
echo "You've completed all of the Ziglings exercises!"
+echo " (That have been written so far.)"
echo