Loops and Blocks and Labels

Just a funny I noticed:

What do you think the output of this program would be:

const std = @import("std");
pub fn main() !void {
	var i: u32 = 0;
	while (i < 6) : (i += 1) b: {
		std.debug.print("BLOCK\n", .{});
		if (i >= 3) break :b;

		std.debug.print("LOOP\n", .{});
	} else {
		std.debug.print("ELSE\n", .{});
	}
}

In other words, does breaking out of the then block break out of the loop?

(obviously you can just run the program I am just curious about everyone’s intuition)

4 Likes

Isn’t the break :b; effectively functioning as a continue?

I would expect 6x BLOCK, the first 3 of which followed by LOOP (until the break/continue takes effect) and then the ELSE

1 Like

It’s the same as if it said continue, right? Don’t show this to andrew, he might remove the keyword for redundancy then :rofl:

5 Likes

Lets quickly flag the post as inappropriate

1 Like

I think it makes sense to remove continue, even though this keyword has been used to implement state machines in switch statements.

Oh no not another cursed language feature… I love it.

:slight_smile: as long as we can rename break to goto…

1 Like