Lesson contents

For loop

Visit each number in order. Print it, move to the next number, and finish after the last value.

Simple example

Print the numbers from one to three.

Start1 → 3
Result1 2 3
lesson.zig
const std = @import("std");

pub fn main() !void {
    for (1..3 + 1) |number| {
        std.debug.print("{any}\n", .{number});
    }
}
Output
1
2
3
Advertisements

Everyday examples

Three friends stand in a ticket queue. Print the next three ticket numbers, starting at 41.

Start41 → 43
Result41 42 43
lesson.zig
const std = @import("std");

pub fn main() !void {
    for (41..43 + 1) |ticket| {
        std.debug.print("{any}\n", .{ticket});
    }
}
Output
41
42
43
Run the example
Terminal
zig run lesson.zig
Official documentation