home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!psinntp!cmcl2!adm!news
- From: stone@hilbert.math.grin.edu (John David Stone)
- Newsgroups: comp.lang.pascal
- Subject: Ok, why doesn't THIS cause an error?
- Message-ID: <34861@adm.brl.mil>
- Date: 4 Jan 93 15:03:24 GMT
- Sender: news@adm.brl.mil
- Lines: 47
-
-
- Alan Mead writes:
-
- > I'm was a little amazed when I accidentally wrote the below
- > bug. I cannot imagine why you are allowed to alter the counter of a
- > for..do loop. I suppose it's too hard for the compiler to check?
- >
- > program bug;
- > var i,j,k : integer;
- >
- > function whatever;
- > { var i,j,k:integer;} { this was the missing line }
- > begin
- > i := 2;
- > for k := 1 to i*x do
- > ...
- > end;
- >
- > begin
- > for i := 1 to TheEnd do
- > whatever;
- > end.
-
- Mead is right. Under the Pascal standard, this program is
- incorrect in two different ways. The assignment "i := 2" is incorrect,
- because i is the control variable of a for-loop in the block within which i
- is declared. And the statement that begins "for k := 1 to i*x" is
- incorrect, because k is not declared in the block closest-containing that
- statement. (In other words: Under the Pascal standard, loop control
- variables must always be declared locally.)
-
- It is not very difficult for a compiler to check these constraints;
- Pascal compilers almost always do so. In fact, the rules are designed in
- part to make such checking easy and fast. In my opinion, the explanation
- for Turbo Pascal's failure to perform the checks is that they're entirely
- negative -- skipping them makes incorrect programs acceptable but does not
- change the semantics of any correctly written program. The designers of
- Turbo Pascal asked themselves, "Why have the compiler spend any time at all
- performing checks whose only result is to irritate programmers who have
- made trivial errors?" There's no good answer to this question unless you
- care a lot more about portability, maintainability, and standardization
- than Borland does.
-
- ------ 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 --------
-