home *** CD-ROM | disk | FTP | other *** search
/ Solo Programadores 22 / SOLO_22.iso / docs / lovelace / lesson5.les < prev    next >
Encoding:
Text File  |  1995-11-21  |  6.9 KB  |  249 lines

  1. <COMMENT This is a lesson file for the Lovelace Ada tutorial>
  2. <COMMENT A program called genlesson is used to transform this file into a set>
  3. <COMMENT of useful HTML files for use by Mosaic & other WWW browsers.>
  4.  
  5. <TUTOR NAME="Lovelace">
  6. <LESSON NUMBER=5>
  7. <AUTHOR NAME="David A. Wheeler" EMAIL="wheeler@ida.org">
  8. <AUTHOR ADDRESS="<A HREF="dwheeler.htm">David A. Wheeler (wheeler@ida.org)</A>">
  9.  
  10. <COMMENT $Id: lesson5.les,v 1.5 1995/05/17 21:25:18 wheeler Exp $ >
  11.  
  12. <COMMENT A lesson is divided into 1 or more "sections".>
  13.  
  14. <SECTION NAME="If Statements">
  15. A sequence_of_statements is simply a sequence of statements.
  16. There are many different kinds of statements; we've already seen assignment
  17. statements and procedure call statements.
  18. We'll now examine a few other kinds of statements, starting with the
  19. <EM>if statement</EM>.
  20. <P>
  21. If statements determine if some condition is true, and then execute some
  22. sequence of statements depending on that determination.
  23. Here's a trivial example that determines if A is equal to B; if it is,
  24. A receives the value of B plus one. If A isn't equal to B, A receives the
  25. value of B minus one:
  26. <P>
  27. <PRE>
  28.  if A = B then
  29.     A := B + 1;
  30.  else
  31.     A := B - 1;
  32.  end if;
  33. </PRE>
  34. <P>
  35. Here's the full <A HREF="bnf.htm">BNF</A> for the if statement:
  36. <PRE>
  37. if_statement ::=
  38.   "if" condition "then"
  39.      sequence_of_statements
  40.   {"elsif" condition "then"
  41.      sequence_of_statements}
  42.   ["else" 
  43.      sequence_of_statements]
  44.   "end if;"
  45. </PRE>
  46. <P>
  47. Like other algorithmic languages, if `condition' is true the
  48. `then' part is executed.
  49. Otherwise, the elsif clauses (if any) are checked in top-to-bottom order,
  50. again looking for a true condition.
  51. Finally, if none of the conditions are true, the `else' clause is executed
  52. (if there's an "else" clause).
  53. <P>
  54. Notice that the keyword "then" is mandatory (it doesn't exist in C or C++).
  55. There is also a case statement (similar to C's `switch') if there are a number
  56. of different possibilities; we won't go into that here.
  57. <P>
  58. <QUESTION Type=Multiple-Choice>
  59. What is the final value of A in the following sequence of statements?
  60. <PRE>
  61.  A := 5;
  62.  B := 6;
  63.  if A > B then
  64.   A := 7;
  65.  else
  66.   A := A - 2;
  67.  end if;
  68. </PRE>
  69. <CHOICES>
  70. <CHOICE ANS=1>3
  71. <CHOICE ANS=2>5
  72. <CHOICE ANS=3>7
  73. </CHOICES>
  74. <ANSWER ANS=1>
  75. <RESPONSES>
  76. <WHEN ANS=1>
  77. Right.
  78. <WHEN ANS=2>
  79. No, sorry. A is changed by the if..then..else.
  80. <WHEN ANS=3>
  81. No, sorry. 5 > 6 is false, so we'd execute the "else" clause.
  82. </RESPONSES>
  83. <SECTION NAME="Loop Forever">
  84. Ada has a number of `looping constructs' for situations where something
  85. must repeat over and over again.
  86. The simplest looping construct just repeats `forever'.
  87. A forever loop simply begins with the phrase ``loop'', has statements to repeat,
  88. and ends with ``end loop;''.  Here's an example of a procedure body
  89. that, when run, repeatedly prints the same message over and over again:
  90. <PRE>
  91. with Text_IO; use Text_IO;
  92. procedure Print_Forever is
  93. begin
  94.  loop
  95.   Put_Line("Hello again!");
  96.  end loop;
  97. end Print_Forever;
  98. </PRE>
  99. <P>
  100. A loop can be terminated through an ``exit'' or an ``exit when'' clause
  101. (similar to C's break statement).
  102. An `exit' causes an immediate exiting of the innermost loop.
  103. An `exit when' clause causes exiting of the innermost loop only if the
  104. following condition is true.
  105. <P>
  106. The `exit when' clause is particularly useful in circumstances
  107. where you must do some action(s)
  108. before you can figure out if the loop has to stop or not.
  109. These are called ``loop-and-a-half'' constructs - you start with "loop",
  110. perform calculations to determine if the loop should stop, use an `exit when'
  111. to exit on that condition, and then work on the result.
  112. <P>
  113. Here's an example of a loop-and-a-half.
  114. This program reads in a list of numbers and prints out
  115. their squares, but stops (without printing) when it reads in the number `0':
  116. <PRE>
  117. with Text_IO, Integer_Text_IO;
  118. use  Text_IO, Integer_Text_IO;
  119. procedure Print_Squares is
  120.  X : Integer;
  121. begin
  122.  loop
  123.   Get(X);
  124.  exit when X = 0;
  125.   Put(X * X);
  126.   New_Line;
  127.  end loop;
  128. end Print_Forever;
  129. </PRE>
  130.  
  131. <QUESTION Type=Multiple-Choice>
  132. What will A's final value be?
  133. <PRE>
  134. A := 1;
  135. loop
  136.  A := A + 1;
  137.  exit when A > 3;
  138. end loop;
  139. </PRE>
  140. <CHOICES>
  141. <CHOICE ANS=1>1
  142. <CHOICE ANS=2>2
  143. <CHOICE ANS=3>3
  144. <CHOICE ANS=4>4
  145. <CHOICE ANS=5>5
  146. </CHOICES>
  147. <ANSWER ANS=4>
  148. <RESPONSES>
  149. <WHEN ANS=1>
  150. No, sorry.
  151. <WHEN ANS=2>
  152. No, sorry.
  153. <WHEN ANS=3>
  154. No, sorry.
  155. <WHEN ANS=4>
  156. Right!
  157. <WHEN ANS=5>
  158. No, sorry.
  159. </RESPONSES>
  160.  
  161. <SECTION NAME="Loop Iteration Schemes">
  162. There are two other common styles of loops that are directly
  163. supported in Ada: <EM>while</EM> loops and <EM>for</EM> loops.
  164. While and for loops are called `iteration schemes'; they are
  165. loops with information prepended to them on the kind of looping
  166. scheme desired.
  167. <P>
  168. The <EM>while</EM> loop is particularly easy;
  169. write a normal loop block, and put in front of the block
  170. the keyword ``while'' and a condition.
  171. Here is an example of a loop that, while N is less than 20,
  172. it prints N and then adds one to it:
  173. <!-- Note that HTML requires the less-than sign to be handled specially -->
  174. <PRE>
  175.  while N < 20
  176.  loop
  177.   Put(N);
  178.   N := N + 1;
  179.  end loop;
  180. </PRE>
  181. <P>
  182. The <EM>for</EM> loop is similar, starting with the keyword ``for''.
  183. A <EM>for</EM> loop assigns a local loop parameter a lower value.
  184. It then repeatedly checks if the loop parameter is less than the
  185. higher value, and if so it executes a sequence of statements and then
  186. adds one to the loop parameter.
  187. Here's an example of a loop that prints "Hello" 20 times:
  188. <PRE>
  189.  for Count in 1 .. 20
  190.  loop
  191.    Put_Line("Hello");
  192.  end loop;
  193. </PRE>
  194. <P>
  195. There are some key points about
  196. <EM>for</EM> loops that need mentioning:
  197. <OL>
  198. <LI>
  199. The variable named in the `for' loop is a local variable visible
  200. only in the statements inside it, and it <EM>cannot</EM> be modified
  201. by the statements inside (you <EM>can</EM> exit a for loop, using the
  202. exit statement we've already seen).
  203. <LI>
  204. Normally a loop always adds one.
  205. The reverse order can be requested by
  206. using the keyword `reverse' after the keyword `in'.
  207. In this case the loop value starts with the upper bound (given second)
  208. and decrements until it is less than the lower bound (given first).
  209. </OL>
  210. <P>
  211. The construct:
  212. <PRE>
  213.   for J in 10 .. 1 loop
  214. </PRE>
  215. <BR>
  216. repeats zero times because 10 is always greater than 1.
  217. <P>
  218. One warning - the construct
  219. <PRE>
  220.   for J in reverse 10 .. 1 loop
  221. </PRE>
  222. repeats zero times as well; Ada considers 10 .. 1 an empty list.
  223. What you probably want instead is:
  224. <PRE>
  225.   for J in reverse 1 .. 10 loop
  226. </PRE>
  227.  
  228. <QUESTION Type=Multiple-Choice>
  229. If you wanted to repeat something exactly ten times, which
  230. iteration construct would be the most straightforward?
  231. <CHOICES>
  232. <CHOICE ANS=1>While loop.
  233. <CHOICE ANS=2>For loop.
  234. <CHOICE ANS=3>A loop without an iteration scheme.
  235. </CHOICES>
  236. <ANSWER ANS=2>
  237. <RESPONSES>
  238. <WHEN ANS=1>
  239. No, sorry.
  240. You <EM>could</EM> do that with a while loop,
  241. but another construct would be simpler.
  242. <WHEN ANS=2>
  243. Right.
  244. <WHEN ANS=3>
  245. No, sorry.
  246. You <EM>could</EM> do that using a loop without an iteration
  247. scheme, but another construct would be simpler.
  248. </RESPONSES>
  249.