Solved

Placement of default statement

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
I could not build boost code, 10 78
sorting efficency of sorting algorithm 30 103
Header of docx file 17 98
Problem with SqlConnection 4 160
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 …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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.

895 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

13 Experts available now in Live!

Get 1:1 Help Now