break
and continue
If you want to exit any kind of loop early, use
break
.
For loop
, this can take an optional expression that becomes the value of the loop
expression.
If you want to immediately start
the next iteration use continue
.
Both continue
and break
can optionally take a label argument which is used
to break out of nested loops:
In this case we break the outer loop after 3 iterations of the inner loop.
Speaker Notes
This slide should take about 5 minutes.
- Note that
loop
is the only looping construct which returns a non-trivial value. This is because itβs guaranteed to be entered at least once (unlikewhile
andfor
loops).