The break Statement
Calling Sequence
Description
Examples
Compatibility
break
break name
break N
break if condition
break name if condition
break N if condition
When a simple break statement is executed, the result is to exit from the innermost repetition (for/while/do) statement within which it occurs.
After exit, execution proceeds with the first statement following the repetition statement.
A multi-level break statement is a break followed by either the name of a for-loop control variable, or a positive integer.
If break is followed by the name of a variable, then execution exits from the innermost for-loop that has that variable as its control variable. Note that the variable is taken literally. It is not evaluated.
If break is followed by an integer N, then execution exits the Nth innermost repetition statement. The statement break 1 is equivalent to just break.
A multi-level break in the two-variable form of for-in loop can refer to either of the two variables.
When a multi-level break is used within a loop expression, the referenced enclosing for must appear within the same expression. One cannot terminate iteration of an enclosing expression.
It is an error if a break appears in a context other than within a repetition statement, or if a qualified break appears where there is no enclosing for-loop using the specified control variable, or there are fewer than N enclosing repetition statements.
A break statement can optionally be followed by the keyword if and a condition to be evaluated. The break statement is executed if and only if the condition evaluates to true.
The statement break if condition is a convenient shorthand for, and semantically equivalent to, if condition then break; end if.
break is a keyword in the Maple language.
Note: Multi-level break statements are not currently supported in 2-D input in the Standard interface.
Find and print the first string in a list:
L := [1, 2, "abc", "a", 7.0, infinity]:
for x in L do if type(x, 'string') then print(x); break; end if; end do:
abc
Print ordered pairs [1,1], [1,2], ..., [4,4], stopping after [2,3]:
for i to 4 do for j to 4 do print([i,j]); if i = 2 and j = 3 then break i; # alternatively, break 2; end if; end do; end do:
1,1
1,2
1,3
1,4
2,1
2,2
2,3
Print each row of a Matrix, stopping after the first row containing a zero.
M, N := 4, 3:
A := LinearAlgebra:-RandomMatrix(M,N):
A[2,2] := 0:
print(A);
−32844−74092−499−31272967
for row to M do print(A[row]); for col to N do break row if A[row,col] = 0; end do; end do:
−32844
−74092
The multi-level and conditional break statements are new in Maple 2021.
The The break Statement command was updated in Maple 2021.
See Also
do
Maple keywords
next
Download Help Document