Control Structures: Difference between revisions

From NARS2000
Jump to navigationJump to search
(New page: <table border="1" cellpadding="5" cellspacing="0" rules="none" summary=""> <tr> <td> <table border="0" cellpadding="5" cellspacing="0" summary=""> <tr valign="top"> <td><a...)
 
No edit summary
Line 5: Line 5:


     <tr valign="top">
     <tr valign="top">
       <td><apll>:for <i>varname</i> :in <i>expr</i> ⋄ ... ⋄ :endfor</apll></td>
       <td><apll>:for <i>varname</i> :in <i>expr</i> ⋄ </apll>...<apll> ⋄ :endfor</apll></td>
       <td></td>
       <td></td>
       <td></td>
       <td></td>
Line 19: Line 19:


     <tr valign="top">
     <tr valign="top">
       <td><apll>:if <i>expr</i> ⋄ ... ⋄ :endif</apll></td>
       <td><apll>:if <i>expr</i> ⋄ </apll>...<apll> ⋄ :endif</apll></td>
       <td></td>
       <td></td>
       <td></td>
       <td></td>
Line 26: Line 26:


     <tr valign="top">
     <tr valign="top">
       <td><apll>:repeat ⋄ ... ⋄ :endrepeat</apll></td>
       <td><apll>:repeat ⋄ </apll>...<apll> ⋄ :endrepeat</apll></td>
       <td></td>
       <td></td>
       <td></td>
       <td></td>
Line 33: Line 33:


     <tr valign="top">
     <tr valign="top">
       <td><apll>:repeat ⋄ ... ⋄ :until <i>expr</i></apll></td>
       <td><apll>:repeat ⋄ </apll>...<apll> ⋄ :until <i>expr</i></apll></td>
       <td></td>
       <td></td>
       <td></td>
       <td></td>
Line 47: Line 47:


     <tr valign="top">
     <tr valign="top">
       <td><apll>:select <i>expr1</i> ⋄ :case <i>expr2</i> ⋄ ... </apll><br/> <apll> ⋄ :caselist <i>expr3</i> ⋄ ... ⋄ :endselect</apll></td>
       <td><apll>:select <i>expr1</i> ⋄ :case <i>expr2</i> ⋄ </apll>...<apll> </apll><br/> <apll> ⋄ :caselist <i>expr3</i> ⋄ </apll>...<apll> ⋄ :endselect</apll></td>
       <td></td>
       <td></td>
       <td></td>
       <td></td>
Line 54: Line 54:


     <tr valign="top">
     <tr valign="top">
       <td><apll>:while <i>expr</i> ⋄ ... ⋄ :endwhile</apll></td>
       <td><apll>:while <i>expr</i> ⋄ </apll>...<apll> ⋄ :endwhile</apll></td>
       <td></td>
       <td></td>
       <td></td>
       <td></td>
Line 61: Line 61:


     <tr valign="top">
     <tr valign="top">
       <td><apll>:while <i>expr1</i> ⋄ ... ⋄ :until <i>expr2</i></apll></td>
       <td><apll>:while <i>expr1</i> ⋄ </apll>...<apll> ⋄ :until <i>expr2</i></apll></td>
       <td></td>
       <td></td>
       <td></td>
       <td></td>
Line 71: Line 71:
</table>
</table>
<br />
<br />
<ul>
<li><p>All Control Structures may be placed all on one line as in <apll>:for I :in {iota}12 ⋄ </apll>...<apll> ⋄ :endfor</apll> (very convenient for use in immediate execution mode), or on multiple lines as in<br />
<apll>:for I :in {iota}12</apll><br />
...<br />
<apll>:endfor</apll></p></li>
<li>Statement labels may be used on any line which contains a Control Structures.  For example,<br />
<apll>[3]  L1::if I &lt; 10 ⋄ </apll>...</li>
<li><p>Each Control Structure that closes with a statement-specific keyword (<apll>:endfor</apll>, <apll>:endif</apll>, <apll>:endrepeat</apll>, <apll>:endselect</apll>, and <apll>:endwhile</apll>), may instead close with the <apll>:end</apll> keyword.</p></li>
</ul>
<br />
== FOR Statement ==
<apll>:for <i>varname</i> :in <i>expr</i></apll><br />
... (Block of statements)<br />
<apll>:endfor</apll>
This statement evaluates <i>expr</i> once, loops through its elements, assigns each successive value to <i>varname</i>, and then executes the statements between <apll>:for</apll> and <apll>:endfor</apll> once for each value in <i>expr</i>.  The <i>expr</i> may be of any type (including character and nested), any rank, and any shape.  If <i>expr</i> is empty, the block of statements is skipped.
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 the 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 <apll>:endfor</apll> keywords.  The latter keyword exits the <apll>:for</apll> loop entirely and transfers control to the statement after the <apll>:endfor</apll> statement.  Typically, these two keywords appear within <apll>:if</apll> statements.
== GOTO Statement ==
<apll>:goto <i>expr</i></apll>
This statement transfers control to the line number specified in <i>expr</i>.  The value of <i>expr</i> must be a numeric simple scalar or one-element vector.  This statement is equivalent to <apll>→<i>expr</i></apll>.
== IF Statement ==
== REPEAT Statement ==
== RETURN Statement ==
<apll>:return</apll>
This statement exits the current function and is equivalent to <apll>→0</apll>.
== SELECT Statement ==
<apll>:select <i>expr1</i></apll><br />
<apll>:case <i>expr2</i></apll><br />
... (Block of statements)<br />
<apll>:caselist <i>expr3</i></apll><br />
... (Block of statements)<br />
<apll>:else</apll><br />
... (Block of statements)<br />
<apll>:endselect</apll>
This statement provides a mechanism for making a choice from multiple cases as to which block of statements is executed.  The <apll>:select</apll> statement expression is evaluated and compared against each successive <apll>:case</apll> expression and the successive items in each <apll>:caselist</apll> expression.  Control is given to the block of statements whose expression matches the <apll>:select</apll> expression.
For the <apll>:case</apll> statement, its expression is compared with the <apll>:select</apll> statement's expression.  The comparison uses the Match function (≡), so it takes into account rank and shape at every level.
For the <apll>:caselist</apll> statement, the successive items in its expression are compared with the <apll>:select</apll> statement's expression.  As with the <apll>:case</apll> statement the comparison uses the Match function.  The <apll>:case</apll> statements allows you to combine multiple selection criteria into one expression.  It is equivalent to the C language use of multiple <b>case</b> statements preceding a block of statements.
Unlike the C language <b>switch</b> statement, the <apll>:case</apll> and <apll>:caselist</apll> expressions need not be constant, and there may be multiple <apll>:case</apll> or <apll>:caselist</apll> statements whose expressions are the same value &mdash; however only the earliest occurrence may be selected.
When the last statement in a block of statements is executed, the system exits the <apll>:select</apll control structure.  The <apll>:leave</apll> statement may be used to exit that block before reaching the last statement, similar to the C language <b>break</b> statement.
Each <apll>:case</apll> and <apll>:caselist</apll> may be followed by zero or more statements which form the block of statements controlled by that statement.
The <apll>:select</apll> statement may be followed by a mixture of zero or more <apll>:case</apll> and <apll>:caselist</apll> blocks.  Optionally, the <apll>:else</apll> statement may appear after all <apll>:case</apll> and <apll>:caselist</apll> statements to handle the case where none of the previous <apll>:case</apll> or <apll>:caselist</apll> expressions matched the <apll>:select</apll> expression, similar to the C language <b>default</b> statement.
== WHILE Statement ==

Revision as of 14:56, 17 August 2008

:for varname :in expr... ⋄ :endfor Loop through the elements of expr assigning each value to varname, and then execute the statements between :for and :endfor.
:goto expr Transfer control to the line number which corresponds to expr.
:if expr... ⋄ :endif Execute the statements between :if and :endif iff expr is 1.
:repeat ⋄ ... ⋄ :endrepeat Repeatedly execute the statements between :repeat and :endrepeat.
:repeat ⋄ ... ⋄ :until expr Repeatedly execute the statements between :repeat and :until until expr is 0.
:return Execute →0.
:select expr1 ⋄ :case expr2...
⋄ :caselist expr3... ⋄ :endselect
Execute a specific block of statements depending upon which :case or :caselist expression matches expr1.
:while expr... ⋄ :endwhile Repeatedly execute the statements between :while and :endwhile while expr is 1.
:while expr1... ⋄ :until expr2 Repeatedly execute the statements between :while and :until while expr1 is 1 and expr2 is 0.


  • 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 statements between :for and :endfor 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 the 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, skipping the statements between the :continue and :endfor keywords. The latter keyword exits the :for loop entirely and transfers control to the statement after the :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

REPEAT Statement

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 block of statements whose expression 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: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 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 matched the :select expression, similar to the C language default statement.

WHILE Statement