TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: ALL
from: NEIL HELLER
date: 1997-08-23 08:59:00
subject: Why the braces?

I always thought that one of the features of C++ is that variables can
be declared ANYWHERE (such as near where you're going to use them). 
However I ran across a situation in MSVC 1.6 with a CPP module that
seems to contradict this.  Can someone explain to me why?
If I write a switch statement thusly, I get compiler errors:
switch (foo) {
    case 1 :
        int bar = 2;
        break;
    case 2 :
        int bar = 3;
        break;
}
However, if I write the code this way there are no errors:
switch (foo) {
    case 1 : {
        int bar = 2;
        break;
    }
    case 2 : {
        int bar = 3;
        break;
    }
}
I was under the assumption that the braces weren't necessary in C++ as
far as declaring variables is concerned.  Why then do I need the
braces?  Do the added braces affect portability in any way?
 * KWQ/2 1.2i * 
--- TMail v1.31.5
---------------
* Origin: Diablo Valley PCUG-BBS, Walnut Creek, CA 510/943-6238 (1:161/55)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.