Check after the first turn
Do the work once, then decide whether another turn is needed. The first value is always printed.
Simple example
Print, then test.
Start1 → 3
Result1
2
3
const std = @import("std");
pub fn main() !void {
var number: i64 = 1;
while (true) {
std.debug.print("{any}\n", .{number});
number += 1;
if (number > 3) break;
}
}
1 2 3
Advertisements
Everyday examples
Print the first waiting ticket even if nobody else is in line. The last ticket today is 41.
StartFirst 41 · last 41
Result41
const std = @import("std");
pub fn main() !void {
var ticket: i64 = 41;
while (true) {
std.debug.print("{any}\n", .{ticket});
ticket += 1;
if (ticket > 41) break;
}
}
41