Article View: net.lang.c++
Article #158Unintuitive semantics for initializing in for statements
From: David Sher
Date: Mon, 14 Apr 1986 09:26
Date: Mon, 14 Apr 1986 09:26
29 lines
1651 bytes
1651 bytes
I have been trying to write an application in c++ (a simple parser
for slot filler files). I have run into various problems but the one
that I think is obviously wierd is the semantics of initializing
in the for statement. Basically my gripe is that:
#include <stream.h>
void
main()
{
for ( int i = 0 ; i < 3 ; i++ ) cout << "Print me\n";
for ( int i = 0 ; i < 3 ; i++ ) cout << "Print me\n"; // error
}
Causes an error on the line labelled error.
I would naturally expect
the scope of a variable defined in a for statement to last for
the rest of the for statement not for the rest of the enclosing
function as it does now. This scoping makes it somewhat hard
to define generic iterating code (one would have to enclose the
for statement in a block. Just put it on my wish list for the
next eddition. (unfortunately some yangs out there may already have
used this misfeature in their programs by then!)
To prevent useless replies, yes I know that this behavior is consistent
with the book, I just wish it wasn't.
--
-David Sher
sher@rochester
seismo!rochester!sher
Technical Details
Message-ID:
<17195@rochester.ARPA>Path:
arc1-ca.pugleaf.net!utzoo!watmath!clyde!cbosgd!gatech!seismo!rochester!sherShow Full Headers
Complete Article Headers
Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: arc1-ca.pugleaf.net!utzoo!watmath!clyde!cbosgd!gatech!seismo!rochester!sher From: sher@rochester.ARPA (David Sher) Newsgroups: net.lang.c++ Subject: Unintuitive semantics for initializing in for statements Message-ID: <17195@rochester.ARPA> Date: Mon, 14-Apr-86 04:26:25 EST Article-I.D.: rocheste.17195 Posted: Mon Apr 14 04:26:25 1986 Date-Received: Sat, 19-Apr-86 14:01:04 EST Reply-To: sher@rochester.UUCP (David Sher) Organization: U of Rochester, CS Dept Lines: 29