Fundamentals7 / 9
Up next

while and for

const std = @import("std");

pub fn main() void {
    const nums = [_]i32{ 1, 2, 3, 4, 5, 6 };
    var sum: i32 = 0;
    // for binds each element by value
    for (nums) |n| {
        if (@rem(n, 2) == 0) sum += n;
    }
    // while with a continue expression acts like a C for-loop
    var i: usize = 0;
    while (i < 3) : (i += 1) {
        std.debug.print("i={d}\n", .{i});
    }
    std.debug.print("even sum {d}\n", .{sum});
}
What you're solving

for over a slice, while with a step