// while loop
while (n < 1024) {
std.debug.print("{} ", .{n});
n *= 2;
}
// while loop with continue expression
// (the continue expression invoked at the end of the loop
// or when an explicit 'continue' is invoked)
while (n < 1024) : (n *= 2) {
std.debug.print("{} ", .{n});
}
continue; // skips the current iteration
break; // exits the loop
// for loop
for (array) |item| {
std.debug.print("{} ", .{item});
}
for (array, 0..) |item, index| {
std.debug.print("{}: {} ", .{index, item});
}
// `Point` is a `type`
const Point = struct {
x: f64,
y: f64,
};
var point = Point{ .x = 1.0, .y = 2.0 }; // create a Point instance
point.x = 3.0; // access and modify fields
// default value
const Character = struct {
level: u8,
health: u16 = 100, // default health is 100
};
// declare `u8` ptr: *u8
// reference `num1`: &num1
var num1: u8 = 5;
const num1_pointer: *u8 = &num1;
var num2: u8 = undefined;
num2 = num1_pointer.*; // dereference `num1_pointer`: num1_pointer.*
num1_pointer.* = 10; // modify the value pointed by `num1_pointer`
// pointer types
var mutable: u8 = 5; // `&mutable` is of type `*u8`
const value: u8 = 5; // `&value` is of type `*const u8`
// const pointer
const p1: *u8 = &locked; // cannot reassign, point to a specific value
var p2: *u8 = &free; // can be reassigned, point to other