E4102

E4102#

Outside of a loop.

This error occurs when using break or continue statements outside of a loop construct. These control flow statements can only be used within loops.

  • break is used to exit a loop early

  • continue is used to skip to the next iteration of a loop

Using these statements outside of a loop body is invalid since there is no loop to break from or continue to the next iteration.

Erroneous example#

///|
pub fn f(xs : Array[Int]) -> Int {
  for i in xs {
    ignore(i)
  } nobreak {
    break 42
  }
  //  ^^^^^^^^ Error: 'break' outside of a loop
}

///|
pub fn g(x : Int) -> Unit {
  continue x
  // ^^^^^^^^ Error: 'continue' outside of a loop
}

Suggestion#

To fix this error, ensure that break and continue are used within a loop construct.

///|
pub fn first_or_default(xs : Array[Int]) -> Int {
  for x in xs {
    if x > 0 {
      break x
    }
  } nobreak {
    0
  }
}

///|
pub fn count_down(from : Int) -> Unit {
  for i = from; i > 0; i = i - 1 {
    if i % 2 == 0 {
      continue
    }
    println(i)
  }
}