home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 3 / TheARMClub_PDCD3.iso / hensa / maths / rlab / docs / RLAB-R~4 < prev    next >
Encoding:
Text File  |  1996-06-28  |  5.2 KB  |  200 lines

  1. <HTML>
  2. <HEAD>
  3. <TITLE> Rlab2 Reference Manual: Program Control Flow</TITLE>
  4. </HEAD>
  5. <BODY>
  6. <A HREF="rlab-ref-3.html"><IMG SRC="prev.gif" ALT="Previous"></A>
  7. <A HREF="rlab-ref-5.html"><IMG SRC="next.gif" ALT="Next"></A>
  8. <A HREF="rlab-ref.html#toc4"><IMG SRC="toc.gif" ALT="Contents"></A>
  9. <HR>
  10. <H2><A NAME="s4">4. Program Control Flow</A></H2>
  11.  
  12.  
  13. <P>This section covers the aspects of the language that control which
  14. program statements get executed, and in what order. Control
  15. statements do not evalute to a numeric value. The available control
  16. statements are the <EM>if-statement</EM>, the <EM>while-statement</EM>,
  17. and the <EM>for-statement</EM>.  A <EM>break-statement</EM> and a
  18. <EM>continue-statement</EM> offer program execution control from
  19. within the if, for, and while statements.</P>
  20.  
  21.  
  22. <H2><A NAME="ss4.1">4.1 If Statement</A></H2>
  23.  
  24.  
  25. <P>The <EM>if-statement</EM> performs a test on the expression in
  26. parenthesis, <EM>expr</EM>, and executes the <EM>statements</EM>
  27. enclosed within braces, if the expression is true (has a non-zero
  28. value). The expression must evaluate to a scalar-expression,
  29. otherwise a run-time error will result.</P>
  30. <P>
  31. <BLOCKQUOTE>
  32. if ( <EM>expr</EM> )
  33. {
  34. <EM>statements</EM>
  35. }
  36. </BLOCKQUOTE>
  37. </P>
  38. <P>The user is free to insert newlines for formatting
  39. purposes. <EM>expr</EM> can be the simplest expression, a constant, or
  40. something more complex, like an assignment, function call, or
  41. relational test(s). Starting with a simple example:</P>
  42. <P>
  43. <BLOCKQUOTE><CODE>
  44. <PRE>
  45. > if ( 1 ) { "TRUE" }
  46. TRUE
  47. > if ( 0 ) { "TRUE" }
  48. </PRE>
  49. </CODE></BLOCKQUOTE>
  50. </P>
  51. <P>An optional <EM>else</EM> keyword is allowed to delineate statements
  52. that will be executed if the expression tests false:</P>
  53. <P>
  54. <BLOCKQUOTE><CODE>
  55. <PRE>
  56. > if ( 0 ) { "TRUE" else "FALSE" }
  57. FALSE
  58. </PRE>
  59. </CODE></BLOCKQUOTE>
  60. </P>
  61. <P>An explicit else-if keyword is not available, however, the else-if
  62. control flow can be reproduced with nested if-statments.</P>
  63. <P>
  64. <BLOCKQUOTE><CODE>
  65. <PRE>
  66. > if ( 0 )
  67.   {
  68.     "true-1"
  69.   else if ( 0 ) {
  70.     "true-2"
  71.   else if ( 1 ) {
  72.     "true-3"
  73.   else
  74.     "else-part"
  75.   }}}
  76. true-3  
  77. </PRE>
  78. </CODE></BLOCKQUOTE>
  79. </P>
  80.  
  81.  
  82.  
  83. <H2><A NAME="ss4.2">4.2 While Statement</A></H2>
  84.  
  85.  
  86. <P>The <EM>while-statement</EM> executes the body of <EM>statements</EM>
  87. until the scalar <EM>expr</EM> is false (has a zero value).</P>
  88. <P>
  89. <BLOCKQUOTE>
  90. while ( <EM>expr</EM> )
  91. {
  92. <EM>statements</EM>
  93. }
  94. </BLOCKQUOTE>
  95. </P>
  96. <P>The while statement is useful when the loop termination conditions
  97. are not know a-priori. When the loop termination condition is know
  98. prior to execution, a for-loop is more efficient. An often used
  99. example is reading a data file, line by line until the end-of-file
  100. is reached:</P>
  101. <P>
  102. <BLOCKQUOTE><CODE>
  103. <PRE>
  104. > while (length (ans = getline ("file")))
  105.   {
  106.     # Operate on the file contents...
  107.   }
  108. </PRE>
  109. </CODE></BLOCKQUOTE>
  110. </P>
  111.  
  112.  
  113.  
  114. <H2><A NAME="ss4.3">4.3 For Statement</A></H2>
  115.  
  116.  
  117. <P>The <EM>for-statement</EM> executes the body of <EM>statements</EM> for
  118. each element of <EM>vector</EM>. The first time the loop-body is
  119. executed <EM>var</EM> is set the value of the first element of
  120. <EM>vector</EM>. The loop is re-executed for each element of
  121. <EM>vector</EM> with <EM>var</EM> set to each subsequent value of
  122. <EM>vector</EM>. If <EM>vector</EM> is empty, then the loop-body is not
  123. executed. </P>
  124. <P>
  125. <BLOCKQUOTE>
  126. for ( <EM>var</EM> in <EM>vector</EM> )
  127. {
  128. <EM>statements</EM>
  129. }
  130. </BLOCKQUOTE>
  131. </P>
  132. <P>The for-loop <EM>vector</EM> can be any type of vector: numeric,
  133. either real or complex, or string. Quite often the for-loop vector
  134. is constructed on the fly using vector notation. Some simple
  135. examples: </P>
  136. <P>
  137. <BLOCKQUOTE><CODE>
  138. <PRE>
  139. > n = 2;
  140. > for ( i in 1:n ) { printf ("%i ", i); } printf("\n");
  141. 1 2 
  142. > x = ["a", "sample", "string"];
  143. > for ( i in x ) { printf ("%s ", i); } printf("\n");
  144. a sample string 
  145. </PRE>
  146. </CODE></BLOCKQUOTE>
  147. </P>
  148. <P>The first part of the previous example shows how a for-loop vector
  149. is often constructed on the fly. The second part demonstrates how a
  150. string vector can be used in a for-loop.</P>
  151.  
  152.  
  153.  
  154.  
  155. <H2><A NAME="ss4.4">4.4 Break Statement</A></H2>
  156.  
  157.  
  158. <P>The <EM>break</EM> statement allows program execution to be transfered
  159. out of a while or for statement. Consequently, break statements are
  160. only valid within for and while loops. When the break statement is
  161. executed, execution of the inner-most loop terminates.</P>
  162. <P>
  163. <BLOCKQUOTE><CODE>
  164. <PRE>
  165. > for ( i in 1:100 ) { if (i == 3) { break } } i
  166.         3  
  167. </PRE>
  168. </CODE></BLOCKQUOTE>
  169. </P>
  170.  
  171.  
  172.  
  173. <H2><A NAME="ss4.5">4.5 Continue Statement</A></H2>
  174.  
  175.  
  176. <P>The <EM>continue</EM> statement forces execution of the next iteration
  177. of the inner-most for or while loop to begin
  178. immediately. Consequently, continue statements are only valid within
  179. for or while loops.</P>
  180. <P>
  181. <BLOCKQUOTE><CODE>
  182. <PRE>
  183. > for ( i in 1:4 ) { if (i == 2) { continue } i }
  184.         1  
  185.         3  
  186.         4  
  187. </PRE>
  188. </CODE></BLOCKQUOTE>
  189. </P>
  190.  
  191.  
  192.  
  193.  
  194. <HR>
  195. <A HREF="rlab-ref-3.html"><IMG SRC="prev.gif" ALT="Previous"></A>
  196. <A HREF="rlab-ref-5.html"><IMG SRC="next.gif" ALT="Next"></A>
  197. <A HREF="rlab-ref.html#toc4"><IMG SRC="toc.gif" ALT="Contents"></A>
  198. </BODY>
  199. </HTML>
  200.