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

  1. Path: sparky!uunet!munnari.oz.au!ariel.ucs.unimelb.EDU.AU!ucsvc.ucs.unimelb.edu.au!lugb!lux!cscmd
  2. Newsgroups: comp.lang.pascal
  3. Subject: Re: Why doesn't this cause an error?
  4. Message-ID: <1993Jan2.104542.27790@lugb.latrobe.edu.au>
  5. From: cscmd@lux.latrobe.edu.au (Mitch Davis)
  6. Date: Sat, 2 Jan 1993 10:45:42 GMT
  7. Sender: news@lugb.latrobe.edu.au (USENET News System)
  8. References: <1993Jan01.220029.27699@crash>
  9. Organization: La Trobe University
  10. Lines: 35
  11.  
  12. In article <1993Jan01.220029.27699@crash> tech@crash.cts.com (Don Bontemps) writes:
  13. >I know there is probably an easy answer to this question, but here is
  14. >a portion of a routine that I am using to convert a lowercase string
  15. >into an uppercase string.
  16. >
  17. >
  18. >    readln(comment);
  19. >    for l := 1 to length of comment do comment[l] := upcase(comment[l]);
  20. >
  21. >If the user doesn't enter anything, this causes the length of comment to
  22. >become zero and what confuses me is that this event does not cause any
  23. >error whatsoever.  I would figure that "for l := 1 to 0" to cause a
  24. >run-time error or some other type of error, but it doesn't.  Why not??
  25.  
  26. In Standard Pascal, the FOR is formally defined to behave in exactly the
  27. same way as:
  28.  
  29.   FOR count := a to b DO BEGIN
  30.     {some statements}
  31.   END;
  32.  
  33.   count := a;
  34.   WHILE a <= b DO BEGIN
  35.     {some statements}
  36.     a := a + 1;
  37.   END;
  38.  
  39. Thus you can see that if b < a, (a<=b) is never true, and so the body of
  40. the WHILE (and hence FOR) statement is never executed.
  41.  
  42. I always used to test for the length of strings FIRST before using a
  43. FOR, until one day I realised the above.  I can always tell code I wrote
  44. before that date!
  45.  
  46. Mitch.
  47.