# V1041 jump_outside_loop

URL: /docs/errors/V1041
Markdown: /docs/errors/V1041.md

> break or continue appears outside a for or while loop (or crosses a function boundary: a break inside a lambda cannot leave the loop around the lambda).

An error from the `resolve` phase. `sakura explain V1041` prints this explanation offline.

`break` or `continue` appears outside a `for` or `while` loop (or crosses a function boundary:
a `break` inside a lambda cannot leave the loop around the lambda). Return a value instead, or
move the jump into the loop body.

Wrong:

```sakura
fn stop() -> Int { break; 1 }
```

Fixed:

```sakura
fn stop() -> Int { for _ in 0..10 { break }; 1 }
```

## Wrong program [#wrong-program]

[`compiler/tests/ui/V1041_jump_outside_loop.sakura`](https://github.com/icantcodefyi/sakuralang/blob/master/compiler/tests/ui/V1041_jump_outside_loop.sakura), which the compiler's ui suite runs through `sakura check`:

```sakura
fn stop() -> Int {
  break
  1
}
```

`sakura check --json` reports this record (pretty-printed; `$DIR` is the fixture's directory):

```json
{
  "schema_version": "1.0",
  "kind": "diagnostic",
  "id": "d:f1dbb8b3dd4c8ccb",
  "code": "V1041",
  "severity": "error",
  "phase": "resolve",
  "message": "`break` outside of a `for` or `while` loop",
  "snapshot": "sha256:$SNAPSHOT",
  "symbol_id": "main::V1041_jump_outside_loop::stop",
  "spans": [
    {
      "uri": "file://$DIR/V1041_jump_outside_loop.sakura",
      "content_hash": "sha256:95b4618fc7120e534d43e6e16c5466203c9283cf3bf7fb67c03255622ba7a384",
      "start_byte": 81,
      "end_byte": 86,
      "role": "primary"
    }
  ],
  "related_ids": [],
  "expected": null,
  "actual": null,
  "obligation": null,
  "counterexample": null,
  "explanation_uri": "sakura://explain/V1041",
  "fixes": []
}
```

## Fixed program [#fixed-program]

[`compiler/tests/ui/V1041_jump_outside_loop.fixed.sakura`](https://github.com/icantcodefyi/sakuralang/blob/master/compiler/tests/ui/V1041_jump_outside_loop.fixed.sakura), which `sakura check` accepts with no diagnostics:

```sakura
fn stop() -> Int {
  for _ in 0..10 { break }
  1
}
```
