break and continue
This document describes the semantics of break and continue in the Harneet language and provides examples.
Overview
break exits the nearest enclosing loop or switch. continue skips to the next iteration of the nearest enclosing loop. - Using
continue while not inside a loop is a runtime error. - If a
break/continue occurs inside a switch within a loop: break exits the switch only (the loop continues). continue propagates to the loop and proceeds to the next iteration.
These semantics are enforced by the evaluator. Internally, a loop depth is tracked to validate continue usage, and break/continue signals are propagated through statement blocks correctly.
Examples
Basic break
| package main
import fmt
fmt.Println("=== basic_break ===")
fmt.Println("Break out of a simple for loop at i == 3")
for var i = 1; i <= 5; i = i + 1 {
if i == 3 {
fmt.Println("hit 3 -> break")
break
}
fmt.Println(i)
}
fmt.Println("Done: basic_break")
|
Basic continue
| package main
import fmt
fmt.Println("=== basic_continue ===")
fmt.Println("Continue skips printing even numbers (prints only odds)")
for var i = 1; i <= 10; i = i + 1 {
if i % 2 == 0 {
continue
}
fmt.Println(i)
}
fmt.Println("Done: basic_continue")
|
Nested loops with break and continue
| package main
import fmt
fmt.Println("=== nested_loops_break_continue ===")
fmt.Println("Outer 1..3, inner 1..3 with continue and break")
for var i = 1; i <= 3; i = i + 1 {
for var j = 1; j <= 3; j = j + 1 {
if j == 2 {
fmt.Println("i=", i, ", j=", j, " -> continue inner")
continue
}
if i == 3 && j == 3 {
fmt.Println("i=", i, ", j=", j, " -> break inner")
break
}
fmt.Println("i=", i, ", j=", j)
}
}
fmt.Println("Done: nested_loops_break_continue")
|
for..in with break and continue
| package main
import fmt
fmt.Println("=== for_in_break_continue ===")
var nums = [1, 2, 3, 4, 5]
fmt.Println("Use continue to skip number 3 and break at 4")
for n in nums {
if n == 3 {
fmt.Println("skip 3 with continue")
continue
}
if n == 4 {
fmt.Println("hit 4 -> break")
break
}
fmt.Println(n)
}
fmt.Println("Done: for_in_break_continue")
|
switch inside loop; while-like pattern in switch body
| package main
import fmt
fmt.Println("=== while_in_switch_and_switch_in_loop ===")
fmt.Println("Switch inside a for loop: use continue and break from inside switch")
for var i = 1; i <= 5; i = i + 1 {
switch i {
case 2, 4 {
fmt.Println("i=", i, " -> continue from switch")
continue
}
case 5 {
fmt.Println("i=", i, " -> break from switch (exits switch; outer loop continues)")
break
}
default {
fmt.Println("i=", i, " default case")
}
// Should reach here for i=1,3
}
fmt.Println("Done: while_in_switch_and_switch_in_loop")
|
Notes
break inside switch does not exit the loop; it exits only the switch. The loop continues. continue always refers to the nearest enclosing loop. If no loop is active, a runtime error is raised. - Within the evaluator,
break/continue are modelled as control-flow signal objects that bubble up through blocks until they are handled by loops, and continue is validated using tracked loop depth.