home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / pascal / 7805 < prev    next >
Encoding:
Internet Message Format  |  1993-01-02  |  2.6 KB

  1. Path: sparky!uunet!haven.umd.edu!mimsy!afterlife!adm!news
  2. From: stone@hilbert.math.grin.edu (John David Stone)
  3. Newsgroups: comp.lang.pascal
  4. Subject: Why doesn't this cause an error?
  5. Message-ID: <34851@adm.brl.mil>
  6. Date: 2 Jan 93 19:52:50 GMT
  7. Sender: news@adm.brl.mil
  8. Lines: 60
  9.  
  10.  
  11.         Don Bontemps writes:
  12.  
  13. >           I would figure that "for l := 1 to 0" to cause a
  14. >  run-time error or some other type of error, but it doesn't.
  15. >  Why not??
  16.  
  17.         Because the Pascal standard specifies that
  18.  
  19.                 for v := e1 to e2 do body
  20.  
  21. shall be equivalent to
  22.  
  23.                 begin
  24.                     temp1 := e1;
  25.                     temp2 := e2;
  26.                     if temp1 <= temp2 then begin
  27.                         v := temp1;
  28.                         body;
  29.                         while v <> temp2 do begin
  30.                             v := succ (v);
  31.                             body
  32.                         end
  33.                     end
  34.                 end
  35.  
  36. where temp1 and temp2 are otherwise unused identifiers of the same type as
  37. v (or, if v is a subrange type, of the host type of v's type), with the
  38. restriction that no assignments to v are permitted within body.
  39.  
  40.         In your example, all that happens is that temp1 is assigned 1,
  41. temp2 is assigned 0, and 1 is found not to be less than or equal to 0.  The
  42. for-loop is not erroneous because its defined equivalent is not erroneous.
  43.  
  44.         Bontemps may be asking a different question:  Why did the language
  45. designers define the for-loop in this way?  My guess is that it's more
  46. convenient for the programmer in the majority of cases.  In particular, in
  47. a loop like
  48.  
  49.                 for itemnumber := 1 to itemcount do begin
  50.                     getitem (item);
  51.                     process (item, result);
  52.                     printresult (result)
  53.                 end
  54.  
  55. it's convenient to have the case in which itemcount is 0 handled correctly
  56. without having to write special-case code, as you would in, say, FORTRAN.
  57. In Bontemps's own example,
  58.  
  59. >       readln(comment);
  60. >       for l := 1 to length of comment do comment[l] := upcase(comment[l]);
  61.  
  62. the for-loop correctly capitalizes the null string without making a fuss.
  63. Of course, if one wants the program to make a fuss, all one has to do is
  64. put in a test just before the for-loop.
  65.  
  66. ------  John David Stone - Lecturer in Computer Science and Philosophy  -----
  67. --------------  Manager of the Mathematics Local-Area Network  --------------
  68. --------------  Grinnell College - Grinnell, Iowa 50112 - USA  --------------
  69. --------  stone@math.grin.edu - (515) 269-3181 - stone@grin1.bitnet  --------
  70.