In one sentence
Exit ends the loop that contains it on the spot, even if its condition would still allow it to go on.
6.7.1 How to write itBreak ; Parts
Exit is a simple statement and ends with ; .It is only valid inside While , Repeat , For or For Each . 6.7.2 How it worksExit leaves the innermost loop that contains it and the program continues after its closing word. It is useful when searching: as soon as the thing appears, there is no need to keep looking.
Program Finding the first one that fits Algorithm Buscar
Dimension numeros [ 5 ] As Integer ;
For i <- 0 To 4 Do
numeros [ i ] <- ( i + 1 ) * 7 ;
EndFor
For Each n In numeros Do
If n > 15 Then
Write "Primero mayor que 15: " , n ;
Break ;
EndIf
Write "Reviso " , n ;
EndFor
EndAlgorithm Output
Reviso 7
Reviso 14
Primero mayor que 15: 21
The program finishes normally.
Figure 6.7-1. Exit cuts the loop on the third round. Cells 28 and 35 are never visited.Five cells with 7, 14, 21, 28 and 35. 7, 14 and 21 are visited; at 21 a red line marks Exit and cells 28 and 35 are never visited.
If the loop has cleanups registered with [Defer ](#control.diferir), they run on exit. There is no word for "skip to the next round".
Under the hood
Exit is a direct jump to the block after the loop. In the intermediate representation it shows up as a jump inside the block of the true branch.
Intermediate representation
module Buscar en
func suspendible @main() -> unit {
b0() mass 2:
stmt.enter @2:3-2:34
%0 = const.i64 5 @2:21-2:22
%1 = array.new array<i64, 1>, %0 @2:3-2:34
stmt.exit @2:3-2:34
stmt.enter @3:3-5:9
%2 = const.i64 0 @3:12-3:13
%3 = const.i64 4 @3:17-3:18
%4 = const.i64 0
%5 = int.add %3, %4 @3:3-5:9
%6 = const.i64 1 @3:3-5:9
%7 = const.i64 0
%8 = int.add %6, %7 @3:3-5:9
loop.check_step %8 @3:3-5:9
jump b1(%2)
b1(%9: i64) mass 0:
%10 = const.i64 0
%11 = int.cmp ge, %8, %10
branch %11, b2(), b3()
b2() mass 0:
%12 = int.cmp le, %9, %5 @3:3-5:9
jump b4(%12)
b3() mass 0:
%13 = int.cmp ge, %9, %5 @3:3-5:9
jump b4(%13)
b4(%14: bool) mass 0:
decide.bool %14 @3:3-5:9
branch %14, b5(), b6()
b5() mass 1:
stmt.enter @4:5-4:30
%15 = const.i64 1 @4:24-4:25
%16 = int.add %9, %15 @4:22-4:23
%17 = const.i64 7 @4:29-4:30
%18 = int.mul %16, %17 @4:27-4:28
array.store %1, %9, %18 @4:5-4:30
stmt.exit @4:5-4:30
%19 = int.add %9, %8 @3:3-5:9
jump b14()
b6() mass 1:
stmt.exit @3:3-5:9
stmt.enter @6:3-12:9
%20 = array.length %1 @6:3-12:9
%21 = const.i64 0
jump b7(%21)
b7(%22: i64) mass 0:
%23 = int.cmp lt, %22, %20 @6:3-12:9
decide.bool %23 @6:3-12:9
branch %23, b8(), b9()
b8() mass 1:
%24 = array.load %1, %22 @6:3-12:9
stmt.enter @7:5-10:10
%25 = const.i64 15 @7:12-7:14
%26 = int.cmp gt, %24, %25 @7:10-7:11
decide.bool %26 @7:5-10:10
branch %26, b11(), b12()
b9() mass 0:
jump b10()
b10() mass 0:
stmt.exit @6:3-12:9
return @13:1-13:13
b11() mass 2:
stmt.enter @8:7-8:40
%27 = const.str "Primero mayor que 15: " @8:13-8:37
out.write %27, %24, newline @8:7-8:40
stmt.exit @8:7-8:40
stmt.enter @9:7-9:13
stmt.exit @9:7-9:13
jump b10() @9:7-9:13
b12() mass 0:
jump b13()
b13() mass 1:
stmt.exit @7:5-10:10
stmt.enter @11:5-11:23
%28 = const.str "Reviso " @11:11-11:20
out.write %28, %24, newline @11:5-11:23
stmt.exit @11:5-11:23
%29 = const.i64 1
%30 = int.add %22, %29 @6:3-12:9
jump b15()
b14() mass 0:
task.yield_check @1:1-1:17
jump b1(%19) @1:1-1:17
b15() mass 0:
task.yield_check @1:1-1:17
jump b7(%30) @1:1-1:17
} In short
Exit ends the innermost loop.It is only valid inside a loop. It runs the pending cleanups of the loop. See also