BlyxDocumentation
Browse chapters
Documentation / Language essentials

Break, continue and loop values

Find the first matching integer and carry it out of a loop.

Choose a stopping rule

while is convenient when the decision to repeat belongs at the top. Here the program must construct a candidate before it can decide whether to stop. A loop expression lets the test live beside the value that will become the answer.

Follow one iteration

The counter starts at 9. The first pass increments it to 10. Its remainder on division by four is nonzero, so continue jumps back to the beginning. The update occurs before continue so the candidate moves forward on every pass.

Return a value from a loop

At 12 the condition for continue is false. break n exits the nearest loop and supplies 12 to the binding found. The semicolon after the loop expression completes the enclosing let declaration. An ordinary break also exits a while loop, without carrying a value.

Distinguish the destinations

continue returns to the next iteration, break leaves the current loop, and return leaves the current function. If your program stops with a step-budget error, trace the path through continue and check that the variable in the condition actually changes.

Try the program

Output Ready
Expected output
12
Open in the full playground →

Practice

Find the first multiple of five strictly greater than nine.

Hint

Keep the starting number and change the divisor.

Worked solution

The first candidate is 10. Its remainder on division by five is zero, so it becomes the loop result.

Find

n % 4

Replace with

n % 5

Output after the change

10