home *** CD-ROM | disk | FTP | other *** search
/ minnie.tuhs.org / unixen.tar / unixen / PDP-11 / Trees / V6 / usr / doc / c / c4 < prev    next >
Encoding:
Text File  |  1975-06-26  |  5.7 KB  |  239 lines

  1. .ul
  2. 9.  Statements
  3. .et
  4. Except as indicated, statements are executed in sequence.
  5. .ms
  6. 9.1  Expression statement
  7. .et
  8. Most statements are expression statements, which have
  9. the form
  10. .dp 1
  11.     expression \fG;
  12. .ed
  13. Usually expression statements are assignments or function
  14. calls.
  15. .ms
  16. 9.2  Compound statement
  17. .et
  18. So that several statements can be used where one is expected,
  19. the compound statement is provided:
  20. .dp 2
  21.     compound-statement:
  22.         { statement-list }
  23.  
  24.     statement-list:
  25.         statement
  26.         statement statement-list
  27. .ed
  28. 9.3  Conditional statement
  29. .et
  30. The two forms of the conditional statement are
  31. .dp 2
  32.     \fGif ( \fIexpression \fG) \fIstatement
  33.     \fGif ( \fIexpression \fG) \fIstatement \fGelse \fIstatement
  34. .ed
  35. In both cases the expression is evaluated
  36. and if it is non-zero, the first substatement
  37. is executed.
  38. In the second case the second substatement is executed
  39. if the expression is 0.
  40. As usual the ``else'' ambiguity is resolved by connecting
  41. an \fGelse\fR with the last encountered elseless \fGif\fR.
  42. .ms
  43. 9.4  While statement
  44. .et
  45. The \fGwhile\fR statement has the form
  46. .dp 1
  47.     \fGwhile ( \fIexpression \fG) \fIstatement
  48. .ed
  49. The substatement is executed repeatedly
  50. so long as the value of the 
  51. expression remains non-zero.
  52. The test takes place before each execution of the
  53. statement.
  54. .ms
  55. 9.5  Do statement
  56. .et
  57. The \fGdo\fR statement has the form
  58. .dp 1
  59.     \fGdo \fIstatement \fGwhile ( \fIexpression \fG) ;
  60. .ed
  61. The substatement is executed repeatedly until
  62. the value of the expression becomes zero.
  63. The test takes place after each execution of the
  64. statement.
  65. .ms
  66. 9.6  For statement
  67. .et
  68. The \fGfor\fR statement has the form
  69. .dp 1
  70. .ft G
  71.     for ( \fIexpression-1\*(op \fG; \fIexpression-2\*(op \fG ; \c
  72. \fIexpression-3\*(op \fG) \fIstatement
  73. .ed
  74. This statement is equivalent to
  75. .dp 5
  76.     expression-1;
  77.     \fGwhile (\fI^expression-2\fG^) {
  78.         \fIstatement
  79.         expression-3^\fG;
  80.     }
  81. .ed
  82. Thus the first expression specifies initialization
  83. for the loop; the second specifies
  84. a test, made before each iteration, such
  85. that the loop is exited when the expression becomes
  86. 0;
  87. the third expression typically specifies an incrementation
  88. which is performed after each iteration.
  89. .pg
  90. Any or all of the expressions may be dropped.
  91. A missing
  92. .ft I
  93. expression-2
  94. .ft R
  95. makes the
  96. implied \fGwhile\fR clause equivalent to ``while(^1^)'';
  97. other missing expressions are simply
  98. dropped from the expansion above.
  99. .ms
  100. 9.7  Switch statement
  101. .et
  102. The \fGswitch\fR statement causes control to be transferred
  103. to one of several statements depending on
  104. the value of an expression.
  105. It has the form
  106. .dp 1
  107.     \fGswitch ( \fIexpression \fG) \fIstatement
  108. .ed
  109. The expression must be \fGint\fR or \fGchar\fR.
  110. The statement is typically compound.
  111. Each statement within the statement
  112. may be labelled with case prefixes
  113. as follows:
  114. .dp 1
  115.     \fGcase \fIconstant-expression \fG:
  116. .ed
  117. where the constant
  118. expression
  119. must be \fGint\fR or \fGchar\fR.
  120. No two of the case constants in a switch
  121. may have the same value.
  122. Constant expressions are precisely defined in \(sc15.
  123. .pg
  124. There may also be at most one statement prefix of the
  125. form
  126. .dp 1
  127.     \fGdefault :
  128. .ed
  129. When the \fGswitch\fR statement is executed, its expression
  130. is evaluated and compared with each case constant in
  131. an undefined order.
  132. If one of the case constants is
  133. equal to the value of the expression,
  134. control is passed to the statement
  135. following the matched case prefix.
  136. If no case constant matches the expression,
  137. and if there is a \fGdefault\fR prefix, control
  138. passes to the prefixed
  139. statement.
  140. In the absence of a \fGdefault\fR prefix
  141. none of the statements in the
  142. switch is executed.
  143. .pg
  144. Case or default prefixes in themselves
  145. do not alter the flow of control.
  146. .ms
  147. 9.8  Break statement
  148. .et
  149. The statement
  150. .dp 1
  151.     \fGbreak ;
  152. .ed
  153. causes termination of the smallest enclosing \fGwhile\fR, \fGdo\fR,
  154. \fGfor\fR, or \fGswitch\fR statement;
  155. control passes to the
  156. statement following the terminated statement.
  157. .ms
  158. 9.9  Continue statement
  159. .et
  160. The statement
  161. .dp 1
  162.     \fGcontinue ;
  163. .ed
  164. causes control to pass to the loop-continuation portion of the
  165. smallest enclosing \fGwhile\fR,
  166. \fGdo\fR, or \fGfor\fR
  167. statement; that is to the end of the loop.
  168. More precisely, in each of the statements
  169. .dp 4
  170. .bG
  171. .ta .5i 2.5i 4.5i
  172.     while (^^.^.^.^^) {    do {    for (^^.^.^.^^) {
  173.       ^.^.^.^      ^.^.^.^      ^.^.^.^
  174.     contin:^;    contin:^;    contin:^;
  175.     }    } while (^^.^.^.^^)^;    }
  176. .ed
  177. .eG
  178. .ta .5i 1i 1.5i 2i 2.5i 3i
  179. a \fGcontinue\fR is equivalent to ``goto contin''.
  180. .ms
  181. 9.10  Return statement
  182. .et
  183. A function returns to its caller by means of
  184. the \fGreturn\fR statement, which has one of the
  185. forms
  186. .dp 2
  187. .ft G
  188.     return ;
  189.     return ( \fIexpression \fG) ;
  190. .ed
  191. In the first case no value is returned.
  192. In the second case, the value of the expression
  193. is returned to the caller
  194. of the function.
  195. If required, the expression is converted,
  196. as if by assignment, to the type of the
  197. function in which it appears.
  198. Flowing off the end of a function is
  199. equivalent to a return with no returned value.
  200. .ms
  201. 9.11  Goto statement
  202. .et
  203. Control may be transferred unconditionally by means of
  204. the statement
  205. .dp 1
  206. .ft G
  207.     goto \fIexpression \fG;
  208. .ed
  209. The expression should be a label (\(sc\(sc9.12, 14.4)
  210. or an expression of type ``pointer to \fGint\fR''
  211. which evaluates to a label.
  212. It is illegal to transfer to a label
  213. not located in the current function
  214. unless some extra-language provision
  215. has been made to adjust
  216. the stack correctly.
  217. .ms
  218. 9.12  Labelled statement
  219. .et
  220. Any statement may be preceded by
  221. label prefixes of the form
  222. .dp 1
  223.     identifier \fG:
  224. .ed
  225. which serve to declare the identifier
  226. as a label.
  227. More details on the semantics of labels are
  228. given in \(sc14.4 below.
  229. .ms
  230. 9.13  Null statement
  231. .et
  232. The null statement has the form
  233. .dp 1
  234.     \fG;
  235. .ed
  236. A null statement is useful to carry a label just before the ``}''
  237. of a compound statement or to supply a null
  238. body to a looping statement such as \fGwhile\fR.
  239.