v4 Control Flow: for, break/continue, && / ||
Lesson, slides, and applied problem sets.
View SlidesLesson
v4 Control Flow: for, break/continue, && / ||
Why v4?
We already have arrays, maps, closures, and GC. Now we add the control-flow features that make real programs less clunky:
forloops with init/cond/postbreakandcontinue- short-circuit boolean operators
&&and||
These features are "language feel" improvements and force deeper compiler reasoning.
1) New syntax
for loop
for (init; cond; post) {
...
}
initruns once before the loop.condis checked before each iteration.postruns after each iteration.- Any part can be omitted.
break / continue
break; // exit the nearest loop
continue; // skip to the next iteration
logical operators
a && b // false if a is falsey, otherwise uses b
c || d // true if c is truthy, otherwise uses d
We keep the result as a boolean (not the original value).
2) Semantics at a glance
break/continueare only valid inside loops.forintroduces a loop scope (init vars live only inside the loop).&&/||are short-circuiting and use truthiness.
3) Compiler implications
forcan be lowered into a while-like shape.break/continueneed jump patching.&&/||require jump-based short-circuit codegen.
4) Our goal
Keep the implementation readable, testable, and modular. This is about clarity, not clever tricks.