🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #2056

Re: Scope of variables declared in if/while/for

#2056
From: John Max Skaller
Date: Wed, 27 Sep 1995 06:12
51 lines
2416 bytes
rmartin@rcmcon.com (Robert Martin) wrote:
>
>In reading the WP I see that the scope of variables declared in 'for'
>statements is now identical with the scope of variables declared in
>'if' and 'while' statements.  i.e. any variable declared in a 'for',
>'if' or 'while' statement is local to the statement, and to the
>controlled statement, but not to the enclosing scope.


   In fact, the WP is not clear, and is probably inconsistent
about all these cases. It isn't clear which scope
such a declared variable is in. Consider:

   if(int i=0)int i;      //#1 ambiguous wording in the WP
   if(int i=0){ int i; }  //#2 ambiguous wording in the WP
   if(int i=0){{int i; }} // definitely legal


The WP mumbles incomprehensibly about not being allowed to
redeclare the control variable in the outer scope of the
controlled statement. This is not supported by the grammar:

  if-statement:
    "if" "(" cond ")" statement

in which statement can be a compound statement. By this grammar
#2 cannot be sensibly disallowed. Instead one would need
an extra production:

     "if" "(" cond ")" "{" statement-list "}"

in which the {} extend the scope of the condition rather
than introducing a new scope NESTED in the scope of the
condition.






-- 
John Max Skaller               voice: 61-2-566-2189
81 Glebe Point Rd              fax:   61-2-660-0850
GLEBE NSW 2037                 email: maxtal@suphys.physics.oz.au
AUSTRALIA                      email: skaller@maxtal.com.au



[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID: <44aghf$b12@oznet03.ozemail.com.au>
Path: arc1-ca.pugleaf.net!engnews2.Eng.Sun.COM!taumet!clamage
References: <MATT.95Sep25095657@physics2.Berkeley.EDU>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!engnews2.Eng.Sun.COM!taumet!clamage
From: John Max Skaller <maxtal@suphys.physics.su.oz.au>
Newsgroups: comp.std.c++
Subject: Re: Scope of variables declared in if/while/for
Date: 27 Sep 1995 06:12:20 GMT
Organization: MAXTAL
Approved: clamage@eng.sun.com (comp.std.c++)
Message-ID: <44aghf$b12@oznet03.ozemail.com.au>
References: <MATT.95Sep25095657@physics2.Berkeley.EDU>
NNTP-Posting-Host: taumet.eng.sun.com
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
X-Nntp-Posting-Host: slsyd4p16.ozemail.com.au
X-Mailer: Mozilla 1.2b6 (Windows; I; 16bit)
Content-Length: 1479
Originator: clamage@taumet