Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Placement of default statement

Posted on 2003-10-24
5
Medium Priority
?
217 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 500 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

783 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