Why does AI use break and continue in EK9?
← What AI Gets Wrong About EK9 · Ref: Q275
AI models trained on C, Java, and Python generate break and continue in EK9 loops. These keywords do not exist in EK9. They are not in the grammar. The compiler cannot parse them.
THE AI MISTAKE
AI generates 'break' to exit a loop early and 'continue' to skip items. Both are invalid EK9.
THE EK9 PHILOSOPHY
For loops in EK9 are designed as complete-iteration constructs. They run from start to finish. If you need selective or partial processing, use stream pipelines instead.
FILTER REPLACES CONTINUE
Where AI writes a loop with continue to skip items, use filter:
cat items | filter by isValid | collect as List of String
Filter selects only matching elements. Non-matching elements are skipped. This is declarative: you say WHAT you want, not HOW to skip.
HEAD REPLACES BREAK
Where AI writes a loop with break to take the first N items, use head:
cat items | head 5 | collect as List of String
Head takes the first N elements and stops. No loop exit needed.
TAIL AND SKIP
For other slicing patterns:
cat items | tail 3 | collect as List of String cat items | skip 2 | collect as List of String
Tail takes the last N. Skip discards the first N.
FOR-IN RUNS FULLY
The for-in loop processes every element:
for item in items stdout.println(item)
This is the intended pattern. Every item is processed. If you need to process only some items, filter first, then iterate.
EVIDENCE
Microsoft (2011): 15% of production bugs in C# involved break and continue errors. Apple SSL bug (2014): duplicated goto bypassed SSL validation. Linux Kernel: 200+ CVE fixes from break-in-wrong-loop bugs.
See Q89 for stream pipelines. See Q125 for head, tail, and skip. See Q144 for the design decision. See Q145 for replacing break and continue. See Q281 for verifying AI code. See Q288 for before and after migration patterns.
Example
defines module qa.ai.mistakes.breakcontinue defines function isLong() as pure -> item as String <- longer <- Boolean() minLength <- 3 longer: length item > minLength defines program BreakContinueDemo() stdout <- Stdout() items <- ["ax", "hello", "by", "world", "no", "stream"] // === FILTER REPLACES CONTINUE === // AI would write: for item in items { if length(item) <= 3 then continue; process(item) } // EK9 way: filter to only long items longItems <- cat items | filter by isLong | collect as List of String for item in longItems stdout.println(`Long item: ${item}`) // === HEAD REPLACES BREAK === // AI would write: for item in items { count++; if count >= 2 then break } // EK9 way: take first 2 firstTwo <- cat items | head 2 | collect as List of String for item in firstTwo stdout.println(`First two: ${item}`) // === COMBINED: FILTER THEN HEAD === firstLong <- cat items | filter by isLong | head 1 | collect as List of String for item in firstLong stdout.println(`First long: ${item}`) // === FOR-IN RUNS FULLY (the intended pattern) === for item in items stdout.println(`All: ${item}`)
Common mistakes
E01071 — EK9 has no continue keyword. It does not exist in the grammar. Use stream pipelines with filter to skip items instead of loops with continue. See ek9 -h E01071 for details.
Incorrect:
continue
Correct:
stdout.println(`Long item: ${item}`)
E01070 — EK9 has no break keyword. It does not exist in the grammar. Use head N in a stream pipeline to take the first N elements instead of loops with break. See ek9 -h E01070 for details.
Incorrect:
break
Correct:
stdout.println(`First two: ${item}`)
Other ways to ask this
- Why does my AI assistant generate break in EK9 loops?
- How do I replace AI-generated break and continue in EK9?
- What does EK9 use instead of break and continue?
Coming from another language?
Java: break and continue in loops, labelled break for nested loops. Python: break and continue in loops. Rust: break and continue with loop labels. Go: break and continue. C/C++: break and continue, major bug source. EK9: NO break or continue, use stream pipelines with filter (skip items), head (take first N), tail (take last N), skip (discard first N). For loops run fully by design.
Keywords: wrong, migrate, hallucination, ai, continue, common-error, loop, stream, exit, head, skip, break, mistake, filter, pitfall, tail