v4 Control Flow: for, break/continue, && / ||

Lesson, slides, and applied problem sets.

View Slides

Lesson

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:

  • for loops with init/cond/post
  • break and continue
  • 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) {
  ...
}
  • init runs once before the loop.
  • cond is checked before each iteration.
  • post runs 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 / continue are only valid inside loops.
  • for introduces a loop scope (init vars live only inside the loop).
  • && / || are short-circuiting and use truthiness.

3) Compiler implications

  • for can be lowered into a while-like shape.
  • break / continue need 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.


Module Items