Solved

Placement of default statement

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

813 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

15 Experts available now in Live!

Get 1:1 Help Now