Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10 beta 3/9/83; site sdcrdcf.UUCP Path: utzoo!linus!security!genrad!grkermit!mit-vax!eagle!harpo!seismo!hao!hplabs!sdcrdcf!jonab From: jonab@sdcrdcf.UUCP (Jonathan Biggar) Newsgroups: net.lang Subject: Re: declarations and nested blocks Message-ID: <749@sdcrdcf.UUCP> Date: Tue, 20-Dec-83 11:51:33 EST Article-I.D.: sdcrdcf.749 Posted: Tue Dec 20 11:51:33 1983 Date-Received: Fri, 23-Dec-83 01:28:56 EST References: <1072@mit-eddie.UUCP> Reply-To: jonab@sdcrdcf.UUCP (Jonathan Biggar) Organization: System Development Corporation, Santa Monica Lines: 46 In article <1072@mit-eddie.UUCP> barmar@mit-eddie.UUCP (Barry Margolin) writes: > >There is a feature of PL/I and Algol which is used in most of their >descendents which is both useful and painful. I am referring to the >fact that the scope of variables includes nested procedures. It allows >one to write simpler (though less clear) code, but it also permits one >to write incorrect code pretty easily. This problem is exemplified in >the following fragment: > > do index = 1 to 10; > call subr (array (index)); > end; > > subr: procedure (thing); > ... > do index = 1 to 5; > ... > end; > end subr; > >Since the programmer forgot to declare a local "index" variable in subr >he ends up with an infinite loop, since index will never reach 10; it >will be set to 5 after each iteration. > >I'm surprised that Ada(tm) does not require some explicit statement to >allow you to inherit from an outer block. Why is this feature >propogated to all new languages, with hardly any change? Ada(tm) does not have this problem, because the index variable in a for loop is implicitly declared to be the type of the range it varies over: loop1: for i in 1..10 loop loop2: for i in 1..5 loop ... end loop loop2; end loop loop1; loop1.i is not the same variable as loop2.i, therefore there can be no infinite loop. (Note that the code inside both loops can reference both variables as loop1.i and loop2.i.) -- Jon Biggar {allegra,burdvax,cbosgd,hplabs,ihnp4,sdccsu3,trw-unix}!sdcrdcf!jonab