Re: switch(){ case arg{}} why?

From: George Hester (hesterloli_at_hotmail.com)
Date: 01/16/05


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.
> 
> 
> 
> 
> 


Relevant Pages

  • Re: switch(){ case arg{}} why?
    ... I can define and initialize in the same statement in a if block but not in a switch block. ... >So those brackets attached to an if block are different then the brackets attached to a switch block. ... you would do that in the outer scope. ...
    (microsoft.public.vc.language)
  • Re: nested ifs
    ... However, when you can use switch, the ... A PHP switch statement only really works on the valueof a single ... Certainly end brackets of nested blocks should be commented to ... I don't have any inline code other than defines in include files. ...
    (comp.lang.php)
  • Re: nested ifs
    ... However, when you can use switch, the ... The php switch ... that's a single expression value...you can have as little or as few ... Certainly end brackets of nested blocks should be commented to ...
    (comp.lang.php)
  • Re: nested ifs
    ... However, when you can use switch, the ... A PHP switch statement only really works on the valueof a single ... Certainly end brackets of nested blocks should be commented to ...
    (comp.lang.php)
  • Re: Basic Stamp controlled pinball machine is the wrong and $ way-but will probably work to some
    ... an effort to create a easily modifyable OS for controlling a pinball ... lmp36fl =1 will flash lamp 36 at the predetermined rate. ... in the jackpot switch block would be: ... obviously when other modes are running different switch block ...
    (rec.games.pinball)

Loading