Control Structures: Difference between revisions
Sudleyplace (talk | contribs) |
Sudleyplace (talk | contribs) |
||
Line 98: | Line 98: | ||
The <i>varname</i> is not localized to the <apll>:for</apll> loop; upon exiting the loop, <i>varname</i> has the last value assigned to it by the <apll>:for</apll> loop. | The <i>varname</i> is not localized to the <apll>:for</apll> loop; upon exiting the loop, <i>varname</i> has the last value assigned to it by the <apll>:for</apll> loop. | ||
To interrupt the flow of control within a block of statements, use the <apll>:continue</apll> or <apll>:leave</apll> keywords. The former keyword transfers control to the end of the block continuing with the next iteration, skipping the statements between the <apll>:continue</apll> and matching <apll>:endfor</apll> keywords. The latter keyword exits the <apll>:for</apll> loop entirely and transfers control to the statement after the matching <apll>:endfor</apll> statement. Typically, these two keywords appear within <apll>:if</apll> statements. | To interrupt the flow of control within a block of statements, use the <apll>:continue</apll> or <apll>:leave</apll> keywords. The former keyword transfers control to the end of the block continuing with the next iteration, thus skipping the statements between the <apll>:continue</apll> and matching <apll>:endfor</apll> keywords. The latter keyword exits the <apll>:for</apll> loop entirely and transfers control to the statement after the matching <apll>:endfor</apll> statement. Typically, these two keywords appear within <apll>:if</apll> statements. | ||
== GOTO Statement == | == GOTO Statement == |
Revision as of 20:58, 20 August 2008
Overview
|
All Control Structures may be placed all on one line as in :for I :in ⍳12 ⋄ ... ⋄ :endfor (very convenient for use in immediate execution mode), or on multiple lines as in
:for I :in ⍳12
...
:endfor- Statement labels may be used on any line which contains a Control Structures. For example,
[3] L1::if I < 10 ⋄ ... Each Control Structure that closes with a statement-specific keyword (:endfor, :endif, :endrepeat, :endselect, and :endwhile), may instead close with the :end keyword.
FOR Statement
:for varname :in expr
... (Block of statements)
:endfor
This statement evaluates expr once, loops through its elements, assigns each successive value to varname, and then executes the block of statements once for each value in expr. The expr may be of any type (including character and nested), any rank, and any shape. If expr is empty, the block of statements is skipped.
The varname is not localized to the :for loop; upon exiting the loop, varname has the last value assigned to it by the :for loop.
To interrupt the flow of control within a block of statements, use the :continue or :leave keywords. The former keyword transfers control to the end of the block continuing with the next iteration, thus skipping the statements between the :continue and matching :endfor keywords. The latter keyword exits the :for loop entirely and transfers control to the statement after the matching :endfor statement. Typically, these two keywords appear within :if statements.
GOTO Statement
:goto expr
This statement transfers control to the line number specified in expr. The value of expr must be a numeric simple scalar or one-element vector. This statement is equivalent to →expr.
IF Statement
:if expr1
... (Block of statements)
:elseif expr2
... (Block of statements)
:else
... (Block of statements)
:endif
This statement conditionally executes a block of statements.
Each expression must evaluate to a Boolean-valued scalar or one-element vector.
Each :if and :elseif statement may be followed by zero or more statements which form the block of statements controlled by that statement.
The :if statement may be followed by zero or more :elseif blocks. Optionally, the :else statement may appear after all :elseif statements to handle the case where none of the previous expressions was true (evaluated to a 1).
Each :if and :elseif statement may be followed by zero or more :andif statements to narrow the conditions under which the following block of statements is executed, or may be followed by zero or more :orif statements to widen the conditions under which the following block of statements is executed. The :andif and :orif statements may not be mixed within any :if or :elseif statement.
REPEAT Statement
:repeat | :repeat | ||
... (Block of statements) | ... (Block of statements) | ||
:endrepeat | :until expr |
This statement executes a block of statements repeatedly (but at least once) until the :until expression is true, or control transfers out of the Control Structure.
In the righthand form above, the block of statements is executed and then the expression is executed. It must evaluate to a Boolean-valued scalar or one-element vector. If the expression is true, the Control Structure terminates and execution continues with the statement after the :until statement; if the expression is false, execution continues at the start of the Control Structure.
The :until statement may be followed by zero or more :andif statements to widen the conditions under which the block of statements is executed, or it may be followed by zero or more :orif statements to narrow the conditions under which the block of statements is executed. The :andif and :orif statements may not be mixed after an :until statement.
To interrupt the flow of control within a block of statements, use the :continue or :leave keywords. The former keyword transfers control to the end of the block continuing with the next repetition, skipping the statements between the :continue and matching :endrepeat or :until keywords. The latter keyword exits the :repeat loop entirely and transfers control to the statement after the matching :endrepeat or :until statement. Typically, these two keywords appear within :if statements.
RETURN Statement
:return
This statement exits the current function and is equivalent to →0.
SELECT Statement
:select expr1
:case expr2
... (Block of statements)
:caselist expr3
... (Block of statements)
:else
... (Block of statements)
:endselect
This statement provides a mechanism for making a choice from multiple cases as to which block of statements is executed. The :select statement expression is evaluated and compared against each successive :case expression and the successive items in each :caselist expression. Control is given to the first block of statements whose corresponding expression (or expression item in the case of :caselist) matches the :select expression.
For the :case statement, its expression is compared with the :select statement's expression. The comparison uses the Match function (≡), so it takes into account rank and shape at every level.
For the :caselist statement, the successive items in its expression are compared with the :select statement's expression. As with the :case statement the comparison uses the Match function. The :case statements allows you to combine multiple selection criteria into one expression. It is equivalent to the C language use of multiple case statements preceding a block of statements.
Unlike the C language switch statement, the :case and :caselist expressions need not be constant, and there may be multiple :case or :caselist statements whose expressions are the same value — however only the earliest occurrence may be selected.
When the last statement in a block of statements is executed, the system exits the :select control structure. The :leave statement may be used to exit that block before reaching the last statement, similar to the C language break statement.
Each :case and :caselist statement may be followed by zero or more statements which form the block of statements controlled by that statement.
The :select statement may be followed by a mixture of zero or more :case and :caselist blocks. Optionally, the :else statement may appear after all :case and :caselist statements to handle the case where none of the previous :case or :caselist expressions match the :select expression — in this context, the :else statement is similar to the C language default statement.
WHILE Statement
:while expr1 | :while expr1 | ||
... (Block of statements) | ... (Block of statements) | ||
:endwhile | :until expr2 |
This statement executes a block of statements repeatedly until the :while expression is false, or the :until expression is true, or control transfers out of the Control Structure.
The expressions must evaluate to a Boolean-valued scalar or one-element vector.
In the righthand form above, the block of statements is executed and then the expression is executed. It must evaluate to a Boolean-valued scalar or one-element vector. If the expression is true, the Control Structure terminates and execution continues with the statement after the :until statement; if the expression is false, execution continues at the start of the Control Structure.
The :while statement may be followed by zero or more :andif statements to narrow the conditions under which the block of statements is executed, or it may be followed by zero or more :orif statements to widen the conditions under which the block of statements is executed. The :andif and :orif statements may not be mixed after a :while statement.
The :until statement may be followed by zero or more :andif statements to widen the conditions under which execution the block of statements is executed, or it may be followed by zero or more :orif statements to narrow the conditions under which the block of statements is executed. The :andif and :orif statements may not be mixed after an :until statement.
To interrupt the flow of control within a block of statements, use the :continue or :leave keywords. The former keyword transfers control to the end of the block continuing with the next repetition, skipping the statements between the :continue and matching :endwhile or :until keywords, but evaluating and acting upon an :until statement, if present. The latter keyword exits the :while loop entirely and transfers control to the statement after the matching :endwhile or :until statement. Typically, these two keywords appear within :if statements.