Solved

Placement of default statement

Posted on 2003-10-24
5
211 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

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

860 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