next - Maple Help

The next Statement

 Calling Sequence next next name next integer next if condition next name if condition next integer if condition

Description

 • When a simple next statement is executed, execution of the current iteration of the innermost repetition (for/while/do) statement is terminated and the next iteration, if any, begins.
 Beginning the next iteration implies that the control variable is incremented, and then the tests for termination (as specified by the to-clause, while-clause, and/or until-clause if present) are applied before proceeding. Thus, an exit from the loop may occur as a result of next statement.
 In effect, execution skips the remainder of the loop's body, and resumes just before the terminating end do or until clause of the repetition statement.
 • A multi-level next statement is a next followed by either the name of a for-loop control variable, or a positive integer.
 • If next is followed by the name of a variable, then execution continues with the next iteration of the innermost for-loop that has that variable as its control variable. Note that the variable is taken literally. It is not evaluated.
 • If next is followed by an integer N, then execution continues with the next iteration of the Nth innermost repetition statement. The statement next 1 is equivalent to just next.
 • A multi-level next in the two-variable form of for-in loop can refer to either of the two variables.
 • When a multi-level next is used within a loop expression, the referenced enclosing for must appear within the same expression. One cannot force the next iteration of an enclosing expression.
 • It is an error if a next is appears in a context other than within a repetition statement, or if a qualifed next appears there is no enclosing for-loop using the specified control variable or there are fewer than N enclosing repetition statements.
 • A next statement may optionally be followed by the keyword if and a condition to be evaluated. The next statement is executed if and only if the condition evaluates to true.
 The statement next if condition is a convenient shorthand for, and semantically equivalent to, if condition then next; end if.
 • In Maple V Release 5.1 and earlier, next was just a Maple symbol. In Maple 6 and later, next is a keyword in the Maple language.

Examples

Print all the non-string entries 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}}\mathbf{next}\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}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}\mathrm{print}\left(x\right)\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\mathbf{end}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{do}$
 ${1}$
 ${2}$
 ${7.0}$
 ${\mathrm{\infty }}$ (1)

Print ordered pairs [1,1], [1,2], ..., [4,4], omitting any [i,j] where j > i.

 > $\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=j\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{next}\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[{2}{,}{1}\right]$
 $\left[{2}{,}{2}\right]$
 $\left[{3}{,}{1}\right]$
 $\left[{3}{,}{2}\right]$
 $\left[{3}{,}{3}\right]$
 $\left[{4}{,}{1}\right]$
 $\left[{4}{,}{2}\right]$
 $\left[{4}{,}{3}\right]$
 $\left[{4}{,}{4}\right]$ (2)

Print the rows of a Matrix, skipping any rows 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}}\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{next}\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}}\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}}\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}{-4}& {99}& {-31}\end{array}\right]$
 $\left[\begin{array}{ccc}{27}& {29}& {67}\end{array}\right]$ (4)

Compatibility

 • The multi-level and conditional next statements are new in Maple 2020.1