statemnt.tex 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. \chapter{Statements}
  2. A statement\index{Statement} is any combination of reserved words and
  3. expressions, and has the syntax \index{Proper statement}
  4. \begin{verbatim}
  5. <statement> ::= <expression>|<proper statement>
  6. \end{verbatim}
  7. A {\REDUCE} program consists of a series of commands which are statements
  8. followed by a terminator:\index{Terminator}\index{Semicolon}
  9. \index{Dollar sign}
  10. \begin{verbatim}
  11. <terminator> ::= ;|$
  12. \end{verbatim}
  13. The division of the program into lines is arbitrary. Several statements
  14. can be on one line, or one statement can be freely broken onto several
  15. lines. If the program is run interactively, statements ending with ; or \$
  16. are not processed until an end-of-line character is encountered. This
  17. character can vary from system to system, but is normally the \key{Return}
  18. key on an ASCII terminal. Specific systems may also use additional keys
  19. as statement terminators.
  20. If a statement is a proper statement\index{Proper statement}, the
  21. appropriate action takes place.
  22. Depending on the nature of the proper statement some result or response may
  23. or may not be printed out, and the response may or may not depend on the
  24. terminator used.
  25. If a statement is an expression, it is evaluated. If the terminator is a
  26. semicolon, the result is printed. If the terminator is a dollar sign, the
  27. result is not printed. Because it is not usually possible to know in
  28. advance how large an expression will be, no explicit format statements are
  29. offered to the user. However, a variety of output declarations are
  30. available so that the output can be produced in different forms. These
  31. output declarations are explained in Section~\ref{sec-output}.
  32. The following sub-sections describe the types of proper statements
  33. \index{Proper statement} in {\REDUCE}.
  34. \section{Assignment Statements}
  35. These statements\index{Assignment} have the syntax
  36. \begin{verbatim}
  37. <assignment statement> ::= <expression> := <expression>
  38. \end{verbatim}
  39. The {\tt <expression>} on the left side is normally the name of a variable, an
  40. operator symbol with its list of arguments filled in, or an array name with
  41. the proper number of integer subscript values within the array bounds. For
  42. example:
  43. \begin{quote}
  44. \begin{tabbing}
  45. {\tt a1 := b + c} \\
  46. {\tt h(l,m) := x-2*y} \hspace{1in} \= (where {\tt h} is an operator) \\
  47. {\tt k(3,5) := x-2*y} \> (where {\tt k} is a 2-dim. array)
  48. \end{tabbing}
  49. \end{quote}
  50. More general assignments\index{Assignment} such as {\tt a+b := c} are also
  51. allowed. The effect of these is explained in Section~\ref{sec-gensubs}.
  52. An assignment statement causes the expression on the right-hand-side to be
  53. evaluated. If the left-hand-side is a variable, the value of the
  54. right-hand-side is assigned to that unevaluated variable. If the
  55. left-hand-side is an operator or array expression, the arguments of that
  56. operator or array are evaluated, but no other simplification done. The
  57. evaluated right-hand-side is then assigned to the resulting expression.
  58. For example, if {\tt A} is a single-dimensional array, {\tt a(1+1) := b}
  59. assigns the value {\tt B} to the array element {\tt a(2)}.
  60. If a semicolon is used as the terminator when an assignment
  61. \index{Assignment} is issued as a command (i.e. not as a part of a group
  62. statement or procedure or other similar construct), the left-hand side
  63. symbol of the assignment statement is printed out, followed by a
  64. ``{\tt :=}'', followed by the value of the expression on the right.
  65. It is also possible to write a multiple assignment statement:
  66. \index{Multiple assignment statement}
  67. \begin{verbatim}
  68. <expression> := ... := <expression> := <expression>
  69. \end{verbatim}
  70. In this form, each {\tt <expression>} but the last is set to the value of
  71. the last {\tt <expression>}. If a semicolon is used as a terminator, each
  72. expression except the last is printed followed by a ``{\tt :=}'' ending
  73. with the value of the last expression.
  74. \subsection{Set Statement}
  75. In some cases, it is desirable to perform an assignment in which {\em both\/}
  76. the left- and right-hand sides of an assignment\index{Assignment} are
  77. evaluated. In this case, the {\tt SET}\ttindex{SET} statement can be used
  78. with the syntax:
  79. \begin{verbatim}
  80. SET(<expression>,<expression>);
  81. \end{verbatim}
  82. For example, the statements
  83. \begin{verbatim}
  84. j := 23;
  85. set(mkid(a,j),x);
  86. \end{verbatim}
  87. assigns the value {\tt X} to {\tt A23}.
  88. \section{Group Statements}
  89. The group statement\index{Group statement} is a construct used where
  90. {\REDUCE} expects a single statement, but a series of actions needs to be
  91. performed. It is formed by enclosing one or more statements (of any kind)
  92. between the symbols {\tt $<<$} and {\tt $>>$}, separated by semicolons or
  93. dollar signs -- it doesn't matter which. The statements are executed one
  94. after another.
  95. Examples will be given in the sections on {\tt IF}\ttindex{IF} and other
  96. types of statements in which the {\tt $<<$} \ldots {\tt $>>$} construct is
  97. useful.
  98. If the last statement in the enclosed group has a value, then that is also
  99. the value of the group statement. Care must be taken not to have a
  100. semicolon or dollar sign after the last grouped statement, if the value of
  101. the group is relevant: such an extra terminator causes the group to have
  102. the value NIL or zero.
  103. \section{Conditional Statements}
  104. The conditional statement\index{Conditional statement} has the following
  105. syntax:
  106. \begin{verbatim}
  107. <conditional statement> ::=
  108. IF <boolean expression> THEN <statement> [ELSE <statement>]
  109. \end{verbatim}
  110. The boolean expression is evaluated. If this is {\em true}, the first
  111. {\tt <statement>} is executed. If it is {\em false}, the second is.
  112. {\it Examples:}
  113. \begin{verbatim}
  114. if x=5 then a:=b+c else d:=e+f
  115. if x=5 and numberp y
  116. then <<ff:=q1; a:=b+c>>
  117. else <<ff:=q2; d:=e+f>>
  118. \end{verbatim}
  119. Note the use of the group statement\index{Group statement}.
  120. \\
  121. Conditional statements associate to the right; i.e.,\ttindex{IF}
  122. \begin{verbatim}
  123. IF <a> THEN <b> ELSE IF <c> THEN <d> ELSE <e>
  124. \end{verbatim}
  125. is equivalent to:
  126. \begin{verbatim}
  127. IF <a> THEN <b> ELSE (IF <c> THEN <d> ELSE <e>)
  128. \end{verbatim}
  129. In addition, the construction
  130. \begin{verbatim}
  131. IF <a> THEN IF <b> THEN <c> ELSE <d>
  132. \end{verbatim}
  133. parses as
  134. \begin{verbatim}
  135. IF <a> THEN (IF <b> THEN <c> ELSE <d>).
  136. \end{verbatim}
  137. If the value of the conditional statement\index{Conditional
  138. statement} is of primary interest, it is often called a conditional
  139. expression instead. Its value is the value of whichever statement was
  140. executed. (If the executed statement has no value, the conditional
  141. expression has no value or the value 0, depending on how it is used.)
  142. {\it Examples:}
  143. \begin{verbatim}
  144. a:=if x<5 then 123 else 456;
  145. b:=u + v^(if numberp z then 10*z else 1) + w;
  146. \end{verbatim}
  147. If the value is of no concern, the {\tt ELSE} clause may be omitted if no
  148. action is required in the {\em false\/} case.
  149. \begin{verbatim}
  150. if x=5 then a:=b+c;
  151. \end{verbatim}
  152. Note: As explained in Section~\ref{sec-boolean},a
  153. if a scalar or numerical expression is used in place of
  154. the boolean expression -- for example, a variable is written there -- the
  155. {\em true\/} alternative is followed unless the expression has the value 0.
  156. \section{FOR Statements}
  157. The {\tt FOR} statement is used to define a variety of program
  158. loops\index{Loop}. Its general syntax is as follows:\ttindex{UNTIL}
  159. \ttindex{DO}\ttindex{PRODUCT}\ttindex{SUM}\ttindex{COLLECT}\ttindex{JOIN}
  160. \begin{small}
  161. \[ \mbox{\tt FOR} \left\{ \begin{array}{@{}ccc@{}}
  162. \mbox{\tt \meta{var} := \meta{number} } \left\{ \begin{array}{@{}c@{}}
  163. \mbox{\tt STEP \meta{number} UNTIL} \\
  164. \mbox{\tt :}
  165. \end{array}
  166. \right\} \mbox{\tt \meta{number}} \\[3mm]
  167. \multicolumn{1}{c}{\mbox{\tt EACH \meta{var}
  168. \(\left\{
  169. \begin{tabular}{@{}c@{}}
  170. IN \\ ON
  171. \end{tabular}
  172. \right\}\)
  173. \meta{list}}}
  174. \end{array}
  175. \right\} \mbox{\tt \meta{action} \meta{exprn}} \]
  176. \end{small}%
  177. %
  178. where
  179. \begin{center}
  180. \tt \meta{action} ::= do|product|sum|collect|join.
  181. \end{center}
  182. The assignment\index{Assignment} form of the {\tt FOR} statement defines an
  183. iteration over the indicated numerical range. If expressions that do not
  184. evaluate to numbers are used in the designated places, an error will
  185. result.
  186. The {\tt FOR EACH}\ttindex{FOR EACH} form of the {\tt FOR} statement is
  187. designed to iterate down a list. Again, an error will occur if a list is
  188. not used.
  189. The action {\tt DO}\ttindex{DO} means that {\tt <exprn>} is simply
  190. evaluated and no value kept; the statement returning 0 in this case (or no
  191. value at the top level). {\tt COLLECT} means that the results of
  192. evaluating {\tt <exprn>} each time are linked together to make a list,
  193. and {\tt JOIN} means that the values of {\tt <exprn>} are themselves
  194. lists that are joined to make one list (similar to {\tt CONC} in Lisp).
  195. Finally, {\tt PRODUCT}\ttindex{PRODUCT} and {\tt SUM}\ttindex{SUM}
  196. form the respective combined value out of the values of {\tt <exprn>}.
  197. In all cases, {\tt <exprn>} is evaluated algebraically within the
  198. scope of the current value of {\tt <var>}. If {\tt <action>} is
  199. {\tt DO}\ttindex{DO}, then nothing else happens. In other cases, {\tt
  200. <action>} is a binary operator that causes a result to be built up and
  201. returned by {\tt FOR}. In those cases, the loop\index{Loop} is
  202. initialized to a default value ({\tt 0} for {\tt SUM},\ttindex{SUM} {\tt
  203. 1} for {\tt PRODUCT},\ttindex{PRODUCT} and an empty list for the other
  204. actions). The test for the end condition is made before any action is
  205. taken. As in Pascal, if the variable is out of range in the assignment
  206. case, or the {\tt <list>} is empty in the {\tt FOR EACH}\ttindex{FOR EACH}
  207. case, {\tt <exprn>} is not evaluated at all.
  208. {\it Examples:}
  209. \begin{enumerate}
  210. \item If {\tt A}, {\tt B} have been declared to be arrays, the following
  211. stores $5^{2}$ through $10^{2}$ in {\tt A(5)} through {\tt A(10)}, and at
  212. the same time stores the cubes in the {\tt B} array:
  213. \begin{verbatim}
  214. for i := 5 step 1 until 10 do <<a(i):=i^2; b(i):=i^3>>
  215. \end{verbatim}
  216. \item As a convenience, the common construction
  217. \begin{verbatim}
  218. STEP 1 UNTIL
  219. \end{verbatim}
  220. may be abbreviated to a colon. Thus, instead of the above we could write:
  221. \begin{verbatim}
  222. for i := 5:10 do <<a(i):=i^2; b(i):=i^3>>
  223. \end{verbatim}
  224. \item The following sets {\tt C} to the sum of the squares of 1,3,5,7,9;
  225. and {\tt D} to the expression {\tt x*(x+1)*(x+2)*(x+3)*(x+4):}
  226. \begin{verbatim}
  227. c := for j:=1 step 2 until 9 sum j^2;
  228. d := for k:=0 step 1 until 4 product (x+k);
  229. \end{verbatim}
  230. \item The following forms a list of the squares of the elements of the list
  231. {\tt \{a,b,c\}:}\ttindex{FOR EACH}
  232. \begin{verbatim}
  233. for each x in {a,b,c} collect x^2;
  234. \end{verbatim}
  235. \item The following forms a list of the listed squares of the elements of the
  236. list {\tt \{a,b,c\}}
  237. (i.e., {\tt \{\{A\verb|^|2\},\{B\verb|^|2\},\{C\verb|^|2\}\}):}
  238. \begin{verbatim}
  239. for each x in {a,b,c} collect {x^2};
  240. \end{verbatim}
  241. \item The following also forms a list of the squares of the elements of
  242. the list {\tt \{a,b,c\},} since the {\tt JOIN} operation joins the
  243. individual lists into one list:\ttindex{FOR EACH}
  244. \begin{verbatim}
  245. for each x in {a,b,c} join {x^2};
  246. \end{verbatim}
  247. \end{enumerate}
  248. The control variable used in the {\tt FOR} statement is actually a new
  249. variable, not related to the variable of the same name outside the {\tt
  250. FOR} statement. In other words, executing a statement {\tt for i:=} \ldots
  251. doesn't change the system's assumption that $i^{2} = -1$.
  252. Furthermore, in algebraic mode, the value of the control variable is
  253. substituted in {\tt <exprn>} only if it occurs explicitly in that
  254. expression. It will not replace a variable of the same name in the value
  255. of that expression. For example:
  256. \begin{verbatim}
  257. b := a; for a := 1:2 do write b;
  258. \end{verbatim}
  259. prints {\tt A} twice, not 1 followed by 2.
  260. \section{WHILE \ldots DO}
  261. The\ttindex{WHILE} {\tt FOR \ldots DO}\ttindex{DO} feature allows easy
  262. coding of a repeated operation in which the number of repetitions is known
  263. in advance. If the criterion for repetition is more complicated, {\tt
  264. WHILE \ldots DO} can often be used. Its syntax is:
  265. \begin{verbatim}
  266. WHILE <boolean expression> DO <statement>
  267. \end{verbatim}
  268. The {\tt WHILE \ldots DO} controls the single statement following {\tt DO}.
  269. If several statements are to be repeated, as is almost always the case,
  270. they must be grouped using the $<<$ \ldots $>>$ or {\tt BEGIN \ldots END}
  271. as in the example below.
  272. The {\tt WHILE} condition is tested each time {\em before\/} the action
  273. following the {\tt DO} is attempted. If the condition is false to begin
  274. with, the action is not performed at all. Make sure that what is to be
  275. tested has an appropriate value initially.
  276. {\it Example:}
  277. Suppose we want to add up a series of terms, generated one by one, until
  278. we reach a term which is less than 1/1000 in value. For our simple
  279. example, let us suppose the first term equals 1 and each term is obtained
  280. from the one before by taking one third of it and adding one third its
  281. square. We would write:
  282. \begin{verbatim}
  283. ex:=0; term:=1;
  284. while num(term - 1/1000) >= 0 do
  285. <<ex := ex+term; term:=(term + term^2)/3>>;
  286. ex;
  287. \end{verbatim}
  288. As long as {\tt TERM} is greater than or equal to ({\tt >=}) 1/1000 it will
  289. be added to {\tt EX} and the next {\tt TERM} calculated. As soon as {\tt
  290. TERM} becomes less than 1/1000 the {\tt WHILE} test fails and the {\tt
  291. TERM} will not be added.
  292. \section{REPEAT \ldots UNTIL}
  293. \ttindex{REPEAT} {\tt REPEAT \ldots UNTIL} is very similar in purpose to
  294. {\tt WHILE \ldots DO}. Its syntax is:
  295. \begin{verbatim}
  296. REPEAT <statement> UNTIL <boolean expression>
  297. \end{verbatim}
  298. (PASCAL users note: Only a single statement -- usually a group statement
  299. -- is allowed between the {\tt REPEAT} and the {\tt UNTIL.)}
  300. There are two essential differences:
  301. \begin{enumerate}
  302. \item The test is performed {\em after\/} the controlled statement (or group of
  303. statements) is executed, so the controlled statement is always executed at
  304. least once.
  305. \item The test is a test for when to stop rather than when to continue, so its
  306. ``polarity'' is the opposite of that in {\tt WHILE \ldots DO.}
  307. \end{enumerate}
  308. As an example, we rewrite the example from the {\tt WHILE \ldots DO} section:
  309. \begin{samepage}
  310. \begin{verbatim}
  311. ex:=0; term:=1;
  312. repeat <<ex := ex+term; term := (term + term^2)/3>>
  313. until num(term - 1/1000) < 0;
  314. ex;
  315. \end{verbatim}
  316. \end{samepage}
  317. In this case, the answer will be the same as before, because in neither
  318. case is a term added to {\tt EX} which is less than 1/1000.
  319. \section{Compound Statements}
  320. \index{Compound statement}Often the desired process can best (or only) be
  321. described as a series of steps to be carried out one after the other. In
  322. many cases, this can be achieved by use of the group statement\index{Group
  323. statement}. However, each step often provides some intermediate
  324. result, until at the end we have the final result wanted. Alternatively,
  325. iterations on the steps are needed that are not possible with constructs
  326. such as {\tt WHILE}\ttindex{WHILE} or {\tt REPEAT}\ttindex{REPEAT}
  327. statements. In such cases the steps of the process must be
  328. enclosed between the words {\tt BEGIN} and {\tt END}\ttindex{BEGIN \ldots
  329. END} forming what is technically called a {\em block\/}\index{Block} or
  330. {\em compound\/} statement. Such a compound statement can in fact be used
  331. wherever a group statement appears. The converse is not true: {\tt BEGIN
  332. \ldots END} can be used in ways that {\tt $<<$} \ldots {\tt $>>$} cannot.
  333. If intermediate results must be formed, local variables must be provided
  334. in which to store them. {\em Local\/} means that their values are deleted as
  335. soon as the block's operations are complete, and there is no conflict with
  336. variables outside the block that happen to have the same name. Local
  337. variables are created by a {\tt SCALAR}\ttindex{SCALAR} declaration
  338. immediately after the {\tt BEGIN}:
  339. \begin{verbatim}
  340. scalar a,b,c,z;
  341. \end{verbatim}
  342. If more convenient, several {\tt SCALAR} declarations can be given one after
  343. another:
  344. \begin{verbatim}
  345. scalar a,b,c;
  346. scalar z;
  347. \end{verbatim}
  348. In place of {\tt SCALAR} one can also use the declarations
  349. {\tt INTEGER}\ttindex{INTEGER} or {\tt REAL}\ttindex{REAL}. In the present
  350. version of {\REDUCE} variables declared {\tt INTEGER} are expected to have
  351. only integer values, and are initialized to 0. {\tt REAL}
  352. variables on the other hand are currently treated as algebraic mode {\tt
  353. SCALAR}s.
  354. {\it CAUTION:} {\tt INTEGER}, {\tt REAL} and {\tt SCALAR} declarations can
  355. only be given immediately after a {\tt BEGIN}. An error will result if
  356. they are used after other statements in a block (including {\tt ARRAY} and
  357. {\tt OPERATOR} declarations, which are global in scope), or outside the
  358. top-most block (e.g., at the top level). All variables declared {\tt
  359. SCALAR} are automatically initialized to zero in algebraic mode ({\tt NIL}
  360. in symbolic mode).
  361. Any symbols not declared as local variables in a block refer to the
  362. variables of the same name in the current calling environment. In
  363. particular, if they are not so declared at a higher level (e.g., in a
  364. surrounding block or as parameters in a calling procedure), their values can
  365. be permanently changed.
  366. Following the {\tt SCALAR}\ttindex{SCALAR} declaration(s), if any, write the
  367. statements to be executed, one after the other, separated by delimiters
  368. (e.g., {\tt ;} or {\tt \$}) (it doesn't matter which). However, from a
  369. stylistic point of view, {\tt ;} is preferred.
  370. The last statement in the body, just before {\tt END}, need not have a
  371. terminator (since the {\tt BEGIN \ldots END} are in a sense brackets
  372. confining the block statements). The last statement must also be the
  373. command {\tt RETURN}\ttindex{RETURN} followed by the variable or
  374. expression whose value is to be the value returned by the procedure. If
  375. the {\tt RETURN} is omitted (or nothing is written after the word
  376. {\tt RETURN}) the procedure will have no value or the value zero, depending
  377. on how it is used (and {\tt NIL} in symbolic mode). Remember to put a
  378. terminator after the {\tt END}.
  379. {\it Example:}
  380. Given a previously assigned integer value for {\tt N}, the following block
  381. will compute the Legendre polynomial of degree {\tt N} in the variable
  382. {\tt X}:
  383. \begin{verbatim}
  384. begin scalar seed,deriv,top,fact;
  385. seed:=1/(y^2 - 2*x*y +1)^(1/2);
  386. deriv:=df(seed,y,n);
  387. top:=sub(y=0,deriv);
  388. fact:=for i:=1:n product i;
  389. return top/fact
  390. end;
  391. \end{verbatim}
  392. \subsection{Compound Statements with GO TO}
  393. It is possible to have more complicated structures inside the {\tt BEGIN
  394. \ldots END}\ttindex{BEGIN \ldots END} brackets than indicated in the
  395. previous example. That the individual lines of the program need not be
  396. assignment\index{Assignment} statements, but could be almost any other
  397. kind of statement or command, needs no explanation. For example,
  398. conditional statements, and {\tt WHILE}\ttindex{WHILE} and {\tt REPEAT}
  399. \ttindex{REPEAT} constructions, have an obvious role in defining more
  400. intricate blocks.
  401. If these structured constructs don't suffice, it is possible to use labels
  402. \index{Label} and {\tt GO} {\tt TO}s\ttindex{GO TO} within a compound
  403. statement,\index{Compound statement} and also to use {\tt RETURN}
  404. \ttindex{RETURN} in places within the block other than just before the
  405. {\tt END}. The following subsections discuss these matters in detail.
  406. For many readers the following example, presenting one possible definition
  407. of a process to calculate the factorial of {\tt N} for preassigned {\tt N}
  408. will suffice:
  409. {\it Example:}
  410. \begin{verbatim}
  411. begin scalar m;
  412. m:=1;
  413. l: if n=0 then return m;
  414. m:=m*n;
  415. n:=n-1;
  416. go to l
  417. end;
  418. \end{verbatim}
  419. \subsection{Labels and GO TO Statements}
  420. \index{Label}\ttindex{GO TO}Within a {\tt BEGIN \ldots END} compound
  421. statement it is possible to label statements, and transfer to them out of
  422. sequence using {\tt GO} {\tt TO} statements. Only statements on the top
  423. level inside compound statements can be labeled, not ones inside
  424. subsidiary constructions like {\tt $<<$} \ldots {\tt $>>$}, {\tt IF} \ldots
  425. {\tt THEN} \ldots , {\tt WHILE} \ldots {\tt DO} \ldots , etc.
  426. Labels and {\tt GO TO} statements have the syntax:
  427. \begin{verbatim}
  428. <go to statement> ::= GO TO <label> | GOTO <label>
  429. <label> ::= <identifier>
  430. <labeled statement> ::= <label>:<statement>
  431. \end{verbatim}
  432. Note that statement names cannot be used as labels.
  433. While {\tt GO TO} is an unconditional transfer, it is frequently used
  434. in conditional statements such as
  435. \begin{verbatim}
  436. if x>5 then go to abcd;
  437. \end{verbatim}
  438. giving the effect of a conditional transfer.
  439. Transfers using {\tt GO TO}s can only occur within the block in which the
  440. {\tt GO TO} is used. In other words, you cannot transfer from an inner
  441. block to an outer block using a {\tt GO TO}. However, if a group statement
  442. occurs within a compound statement, it is possible to jump out of that group
  443. statement to a point within the compound statement using a {\tt GO TO}.
  444. \subsection{RETURN Statements}
  445. The value corresponding to a {\tt BEGIN \ldots END} compound statement,
  446. \ttindex{BEGIN \ldots END} such as a procedure body, is normally 0 ({\tt
  447. NIL} in symbolic mode). By executing a {\tt RETURN}\ttindex{RETURN}
  448. statement in the compound statement a different value can be returned.
  449. After a {\tt RETURN} statement is executed, no further statements within
  450. the compound statement are executed.
  451. {\tt Examples:}
  452. \begin{verbatim}
  453. return x+y;
  454. return m;
  455. return;
  456. \end{verbatim}
  457. Note that parentheses are not required around the {\tt x+y}, although they
  458. are permitted. The last example is equivalent to {\tt return 0} or {\tt
  459. return nil}, depending on whether the block is used as part of an
  460. expression or not.
  461. Since {\tt RETURN}\ttindex{RETURN} actually moves up only one
  462. block\index{Block} level, in a sense the casual user is not expected to
  463. understand, we tabulate some cautions concerning its use.
  464. \begin{enumerate}
  465. \item {\tt RETURN} can be used on the top level inside the compound
  466. statement, i.e. as one of the statements bracketed together by the {\tt
  467. BEGIN \ldots END}\ttindex{BEGIN \ldots END}
  468. \item {\tt RETURN} can be used within a top level {\tt $<<$} \ldots {\tt
  469. $>>$} construction within the compound statement. In this case, the {\tt
  470. RETURN} transfers control out of both the group statement and the compound
  471. statement.
  472. \item {\tt RETURN} can be used within an {\tt IF} \ldots {\tt THEN} \ldots
  473. {\tt ELSE} \ldots on the top level within the compound statement.
  474. \end{enumerate}
  475. NOTE: At present, there is no construct provided to permit early
  476. termination of a {\tt FOR}\ttindex{FOR}, {\tt WHILE}\ttindex{WHILE},
  477. or {\tt REPEAT}\ttindex{REPEAT} statement. In particular, the use of
  478. {\tt RETURN} in such cases results in a syntax error. For example,
  479. \begin{verbatim}
  480. begin scalar y;
  481. y := for i:=0:99 do if a(i)=x then return b(i);
  482. ...
  483. \end{verbatim}
  484. will lead to an error.