Solved

Placement of default statement

Posted on 2003-10-24
5
208 Views
Last Modified: 2010-04-02
In the following code:


switch (someVar)
{
     case 1:
    {
        //
        break;
    }
    default:
    {
        //
        break;
    }
    case 2:
    {
          //
          break:
     }

will case 2 ever execute, or would the default statement catch the program flow beforehand?
0
Comment
Question by:mattjsimps
  • 3
  • 2
5 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 9613150
case 2 will execute if the variable is equal to two, regardless of the location of the default
0
 
LVL 1

Author Comment

by:mattjsimps
ID: 9613180
I thought that was the case....Code reviewer doesn't believe me though. Anyone else?
0
 
LVL 30

Expert Comment

by:Axter
ID: 9613190
All you have to do is run a small test to prove it:

int main(int argc, char* argv[])
{
      cout << "Hello World!" << endl;

      int x;
      cin >> x;

      switch(x)
      {
      case 1:
            printf("1\n");
            break;
      default:
            printf("d\n");
            break;
      case 2:
            printf("2\n");
            break;
      }
      
      system("pause");
      return 0;
}

0
 
LVL 30

Accepted Solution

by:
Axter earned 125 total points
ID: 9613236
If you're reviewer does not believe the out come of a test result, then tell him to read the C++ standard which states the following:

ISO/IEC 14882:1998(E)
Sections 6.4.2  (Pargraph 5 and 6)

5 When the switch statement is executed, its condition is evaluated and compared with each case constant.
If one of the case constants is equal to the value of the condition, control is passed to the statement following
the matched case label. If no case constant matches the condition, and if there is a default label,
control passes to the statement labeled by the default label. If no case matches and if there is no default
then none of the statements in the switch is executed.

6 case and default labels in themselves do not alter the flow of control, which continues unimpeded
across such labels. To exit from a switch, see break, 6.6.1. [Note: usually, the substatement that is the
subject of a switch is compound and case and default labels appear on the toplevel
statements contained
within the (compound) substatement, but this is not required. Declarations can appear in the substatement
of a switchstatement.
]
0
 
LVL 1

Author Comment

by:mattjsimps
ID: 9613261
Unfortunatly, some of our servers are down, and i cant get onto a target to run some test code. I also figure, if i ask a dumb question like this here, i get some nice person posting the relavent section of the standard.

;-)
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now