home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!haven.umd.edu!mimsy!afterlife!adm!news
- From: stone@hilbert.math.grin.edu (John David Stone)
- Newsgroups: comp.lang.pascal
- Subject: Why doesn't this cause an error?
- Message-ID: <34851@adm.brl.mil>
- Date: 2 Jan 93 19:52:50 GMT
- Sender: news@adm.brl.mil
- Lines: 60
-
-
- Don Bontemps writes:
-
- > 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??
-
- Because the Pascal standard specifies that
-
- for v := e1 to e2 do body
-
- shall be equivalent to
-
- begin
- temp1 := e1;
- temp2 := e2;
- if temp1 <= temp2 then begin
- v := temp1;
- body;
- while v <> temp2 do begin
- v := succ (v);
- body
- end
- end
- end
-
- where temp1 and temp2 are otherwise unused identifiers of the same type as
- v (or, if v is a subrange type, of the host type of v's type), with the
- restriction that no assignments to v are permitted within body.
-
- In your example, all that happens is that temp1 is assigned 1,
- temp2 is assigned 0, and 1 is found not to be less than or equal to 0. The
- for-loop is not erroneous because its defined equivalent is not erroneous.
-
- Bontemps may be asking a different question: Why did the language
- designers define the for-loop in this way? My guess is that it's more
- convenient for the programmer in the majority of cases. In particular, in
- a loop like
-
- for itemnumber := 1 to itemcount do begin
- getitem (item);
- process (item, result);
- printresult (result)
- end
-
- it's convenient to have the case in which itemcount is 0 handled correctly
- without having to write special-case code, as you would in, say, FORTRAN.
- In Bontemps's own example,
-
- > readln(comment);
- > for l := 1 to length of comment do comment[l] := upcase(comment[l]);
-
- the for-loop correctly capitalizes the null string without making a fuss.
- Of course, if one wants the program to make a fuss, all one has to do is
- put in a test just before the for-loop.
-
- ------ John David Stone - Lecturer in Computer Science and Philosophy -----
- -------------- Manager of the Mathematics Local-Area Network --------------
- -------------- Grinnell College - Grinnell, Iowa 50112 - USA --------------
- -------- stone@math.grin.edu - (515) 269-3181 - stone@grin1.bitnet --------
-