Re: switch(){ case arg{}} why?
From: George Hester (hesterloli_at_hotmail.com)
Date: 01/16/05
- Next message: Duane Hebert: "Re: switch(){ case arg{}} why?"
- Previous message: George Hester: "Re: switch(){ case arg{}} why?"
- In reply to: Duane Hebert: "Re: switch(){ case arg{}} why?"
- Next in thread: Duane Hebert: "Re: switch(){ case arg{}} why?"
- Reply: Duane Hebert: "Re: switch(){ case arg{}} why?"
- Reply: George Hester: "Re: switch(){ case arg{}} why?"
- Messages sorted by: [ date ] [ thread ]
Date: Sun, 16 Jan 2005 05:54:06 -0500
Duane that won't work:
switch(spoo) {
case 1:
int x;
case 2:
case 3:
x = 42;
case 4:
x = 43: break;
case 5:
x = 46: return;
default:
break;
}
that doesn't work becase you could go into case 3 and x will not have been defined. But this will work:
void main(){
int spoo;
do{
switch(spoo) {
int x;
case 1:
case 2:
case 3:
x = 42;
case 4:
x = 43; break;
case 5:
x = 46; return;
default:
break;
}
}while(spoo<50);
}
try it. That will compile and run just fine. Just won't get any output.
--
George Hester
_________________________________
"Duane Hebert" <spoo@flarn2.com> wrote in message news:O1EyfKw#EHA.3236@TK2MSFTNGP15.phx.gbl...
>
> "George Hester" <hesterloli@hotmail.com> wrote in message news:%23BNC8km%23EHA.612@TK2MSFTNGP09.phx.gbl...
> >Hmm now that's weird. I can define and initialize in the same statement in a if block but not in a switch block. Well bligjmy.
> >So those brackets attached to an if block are different then the brackets attached to a switch block. I don't like that.
> >Inconsistency. But that's the way it is.
>
> You can define and intialize the variable in a switch if it's within a case and bracketed.
> This is the same as if it was in an if statement. Without the brackets in a case, it would
> be the same as trying to declare a variable in one if statement and having it valid
> in consecutive ones. With nested if statements, you would do that in the outer scope.
> For a switch, you can do that in an outer scope as well. I don't see the inconsistency.
>
> Additionally, if this was allowed in a switch, consider:
>
> switch(spoo) {
> case 1:
> int x;
> case 2:
> case 3:
> x = 42;
> case 4:
> x = 43: break;
> case 5:
> x = 46: return;
> default:
> break;
> }
>
> What would be the scope of x? I find it less confusing if it's
> bracketed. It's always been this way so I guess that I'm just
> used to it.
>
>
>
>
>
- Next message: Duane Hebert: "Re: switch(){ case arg{}} why?"
- Previous message: George Hester: "Re: switch(){ case arg{}} why?"
- In reply to: Duane Hebert: "Re: switch(){ case arg{}} why?"
- Next in thread: Duane Hebert: "Re: switch(){ case arg{}} why?"
- Reply: Duane Hebert: "Re: switch(){ case arg{}} why?"
- Reply: George Hester: "Re: switch(){ case arg{}} why?"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|
Loading