# break / continue

> break leaves the enclosing loop; continue skips the rest of this pass. Inside a switch, break leaves the SWITCH, not the loop around it — the one place this trips people up.

<!-- id: function-break-continue · area: function · stability: stable · html: https://osysharp.com/reference/function/break-continue/ -->

## Summary        {#summary}
`break` leaves the enclosing loop. `continue` abandons this pass and goes to the next one. Both behave exactly as in
C# — including the part that catches people out: **inside a `switch`, `break` leaves the switch, not the loop around
it.**

## Signature      {#signature}
```osy syntax
break;      // stop looping
continue;   // skip the rest of this pass
```

## Description    {#description}

### `continue` — skip this one   {#continue}
```osy title="skipping the rows you do not care about" test app=function-break-continue
entity Order {
  [Required] string Code;
  decimal Total;
  bool Cancelled;
}

decimal LiveTotal() {
  var total = 0m;
  foreach (var o in Order.Where(x => x.Total > 0).ToList()) {
    if (o.Cancelled) { continue; }    // not this one — next
    total += o.Total;
  }
  return total;
}
```

### `break` — stop entirely   {#break}
```osy title="stopping at the first match" test app=function-break-continue
string FirstBig(decimal threshold) {
  var found = "";
  foreach (var o in Order.Where(x => x.Total > 0).ToList()) {
    if (o.Total >= threshold) {
      found = o.Code;
      break;                          // done — no point looking further
    }
  }
  return found;
}
```

### Why didn't `break` leave my loop? — the `switch` trap   {#switch-trap}
This is the one to remember. Inside a `switch` that sits in a loop, `break` ends the **switch**, and execution
continues after it — *inside the same pass of the loop*. It does not leave the loop.

`continue`, by contrast, passes straight through the switch and continues the **loop**:

```osy title="break exits the switch; continue continues the loop" test app=function-break-continue
int SumNonZero(int[] xs) {
  var total = 0;
  for (int i = 0; i < xs.Length; i++) {
    switch (xs[i]) {
      case 0: continue;               // skips to the next i — the loop's next pass
      default: break;                 // leaves the SWITCH; falls through to the += below
    }
    total += xs[i];
  }
  return total;
}
```

If you want to leave the loop from inside a switch, you need a flag, or to restructure the loop — exactly as in C#.
This is not a wart we introduced; it is C#'s rule, and we kept it rather than invent a different one you would have to
learn twice.

## See also       {#see-also}
- [foreach](https://osysharp.com/reference/function/foreach/) · [while](https://osysharp.com/reference/function/while-loop/) · [for](https://osysharp.com/reference/function/for-loop/) — the loops these act on
- [switch](https://osysharp.com/reference/function/switch/) — where `break` means something different
