break - Maple Help

The break Statement

 Calling Sequence break break name break N break if condition break name if condition break N if condition

Description

 • 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.

Examples

Find and print the first string in a list:

 > $L≔\left[1,2,"abc","a",7.0,\mathrm{\infty }\right]:$
 > $\mathbf{for}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}x\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{in}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}L\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{do}\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}\mathbf{if}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathrm{type}\left(x,'\mathrm{string}'\right)\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{then}\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}\mathrm{print}\left(x\right);\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}\mathbf{break}\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}\mathbf{end}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{if}\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\mathbf{end}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{do}:$
 ${"abc"}$ (1)

Print ordered pairs [1,1], [1,2], ..., [4,4], stopping after [2,3]:

 > $\mathbf{for}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}i\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{to}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}4\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{do}\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}\mathbf{for}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}j\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{to}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}4\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{do}\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}\mathrm{print}\left(\left[i,j\right]\right);\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}\mathbf{if}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}i=2\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{and}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}j=3\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{then}\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}\mathbf{break}\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}\mathbf{end}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{if}\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}\mathbf{end}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{do}\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\mathbf{end}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{do}:$
 $\left[{1}{,}{1}\right]$
 $\left[{1}{,}{2}\right]$
 $\left[{1}{,}{3}\right]$
 $\left[{1}{,}{4}\right]$
 $\left[{2}{,}{1}\right]$
 $\left[{2}{,}{2}\right]$
 $\left[{2}{,}{3}\right]$ (2)

Print each row of a Matrix, stopping after the first row containing a zero.

 > $M,N≔4,3:$
 > $A≔\mathrm{LinearAlgebra}:-\mathrm{RandomMatrix}\left(M,N\right):$
 > $A\left[2,2\right]≔0:$
 > $\mathrm{print}\left(A\right)$
 $\left[\begin{array}{ccc}{-32}& {8}& {44}\\ {-74}& {0}& {92}\\ {-4}& {99}& {-31}\\ {27}& {29}& {67}\end{array}\right]$ (3)
 > $\mathbf{for}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathrm{row}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{to}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}M\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{do}\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}\mathrm{print}\left(A\left[\mathrm{row}\right]\right);\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}\mathbf{for}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathrm{col}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{to}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}N\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{do}\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}\mathbf{if}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}A\left[\mathrm{row},\mathrm{col}\right]=0\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{then}\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}\mathbf{break}\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}\mathbf{end}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{if}\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}\mathbf{end}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{do}\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\mathbf{end}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{do}:$
 $\left[\begin{array}{ccc}{-32}& {8}& {44}\end{array}\right]$
 $\left[\begin{array}{ccc}{-74}& {0}& {92}\end{array}\right]$ (4)

Compatibility

 • The multi-level and conditional break statements are new in Maple 2021.
 • The The break Statement command was updated in Maple 2021.