Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Declarations within a switch statement

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hi,I'm a beginner.Declarations in switch statement must be reachable and It must not be possible to bypass them.What I don't understand is why in Case 2,where there are no braces,declaration could get bypassed?I can't see any reason why It would be bypassed.Can someone explain it?

switch(test)
{
case 1:
{
int j=2;
cout <<test +j;
break;
}

case 2:
cout<<endl << test;
int m=4; //how can this one
break; get bypassed?
}
Also,I wanted to use output manipulator BOOLALPHA
to display boolean values on the screen,but
compiler simply ignores the statement.
cout<<boolalpha;
Thank you for helping me out

 
Hi, your switch statement should look like this:

switch(test)
{
case 1:
// do something
break;
case 2:
// do something else
break;
}

note there are no curly braces to each case as in your sample code.

 
As mentioned, you wouldn't have to use the curly braces. The same thing happens whether you use them or not.

I suspect you may be using them simply to test your theory. The way a switch statement works is a lot like an if() statement. For instance:
[ignore]
char* pMyString = NULL;
BOOL bFlag2 = TRUE;

if (pMyString != NULL && bFlag2 == TRUE)
{
// do something
}
[/ignore]
Here the program tests the first condition (pMyString != NULL). Since the test fails, it doesn't even bother trying the second test.

A switch statement is similar in a way. When a particular case fails, whatever statements follow until the next case (or ending bracket) is completely bypassed.

The upshot is that you need to declare any variables outside of the switch statement... or the Microsoft compiler probably won't like it.

Hope this helps. X-)
 
I think the curly braces are needed if there are any variable/constant declarations withing the case statements. I have had problems with this in the past. The int m portion of the code will need to keep the curly braces. I, however keep my break outside the curly braces for readablilty purposes.

Matt
 
I might be wrong here, but I think (going back to the ol' Turbo C++ days) that curly braces define their own variable scope, being one stage down the tracks from whatever created them...

So, this might be considered naughty...

Code:
  switch (i){

    case 1:
      // Do something
      break;

    case 2:
      int m=10;
      //Do something else
      break;
  
}

The problem is that since the variable m is declared within the curly braces of the switch statement, it is not visible beyond them. The soluction of course is to declare the variable outside of the switch statement, and define it inside (if you like) - like this...


Code:
  int m;

  switch (i){

    case 1:
      // Do something
      break;

    case 2:
      m=10;
      //Do something else
      break;
  
}


I hope that threw a little light on the subject!

Martin
 
Hmm... I was aware that curly braces limit the scope of any variables contained within them.

But, thanks Zyrenthian. What I didn't know was that you *could* define variables within a case section *if* you enclose them in curly braces.

Since the compiler always complained whenever I put variables within the switch/case group, I always moved them just outside of the switch/case group to &quot;shut up&quot; the compiler.

Yes, mmilan, that's the naughty :-9 behavior I was guilty of and the action I always took to solve it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top