home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!munnari.oz.au!ariel.ucs.unimelb.EDU.AU!ucsvc.ucs.unimelb.edu.au!lugb!lux!cscmd
- Newsgroups: comp.lang.pascal
- Subject: Re: Why doesn't this cause an error?
- Message-ID: <1993Jan2.104542.27790@lugb.latrobe.edu.au>
- From: cscmd@lux.latrobe.edu.au (Mitch Davis)
- Date: Sat, 2 Jan 1993 10:45:42 GMT
- Sender: news@lugb.latrobe.edu.au (USENET News System)
- References: <1993Jan01.220029.27699@crash>
- Organization: La Trobe University
- Lines: 35
-
- In article <1993Jan01.220029.27699@crash> tech@crash.cts.com (Don Bontemps) writes:
- >I know there is probably an easy answer to this question, but here is
- >a portion of a routine that I am using to convert a lowercase string
- >into an uppercase string.
- >
- >
- > readln(comment);
- > for l := 1 to length of comment do comment[l] := upcase(comment[l]);
- >
- >If the user doesn't enter anything, this causes the length of comment to
- >become zero and what confuses me is that this event does not cause any
- >error whatsoever. I would figure that "for l := 1 to 0" to cause a
- >run-time error or some other type of error, but it doesn't. Why not??
-
- In Standard Pascal, the FOR is formally defined to behave in exactly the
- same way as:
-
- FOR count := a to b DO BEGIN
- {some statements}
- END;
-
- count := a;
- WHILE a <= b DO BEGIN
- {some statements}
- a := a + 1;
- END;
-
- Thus you can see that if b < a, (a<=b) is never true, and so the body of
- the WHILE (and hence FOR) statement is never executed.
-
- I always used to test for the length of strings FIRST before using a
- FOR, until one day I realised the above. I can always tell code I wrote
- before that date!
-
- Mitch.
-