answersLogoWhite

0


Best Answer

The purpose of a switch statement is to execute one of several sections of code, depending on the value of an integral expression.

The syntax of a switch statement can be stated as follows:

switch(expression)

{

case(constant_expression_1): statement

case(constant_expression_2): statement

//..

case(constant_expression_n): statement

[default: statement]

}

The ellipses around the constant expressions are optional and may be omitted.

The switch statement body consists of one or more case labels and an optional default label (the case labels are also optional, but a switch statement would be meaningless without them). The default statement, if present, need not be the final label in a switch statement. Case and default labels may not appear outside of a switch statement, as they are not treated the same as goto labels.

The constant_expression in each case label is converted to the type of the switch expression and is compared with the switch expression for equality. Control is immediately passed to the labelled statement whose constant_expression matches the value of the switch expression.

Once a label gains control, execution proceeds from that point on and is not impeded by any subsequent case or default labels. However, a break statement may be used to interrupt execution at any time, which will transfer control to the statement immediately following the switch statement body. You may also use a return statement to return control to the calling function, or a goto statement. However, note that while a goto label is not the same as a case label, you may use goto labels inside a switch statement if required.

Here's an example demonstrating how code flows through a switch statement:

int x;

switch(x=rand()%10) // x is a random number in the range 0 to 9.

{

case(0): x=42; break;

case(1):

case(2): x=33; break;

case(3): x=100;

default: x=0;

}

If x were 0, then execution passes to case(0) where the value 42 is assigned to x and the switch exits.

If x were 1 or 2, then 33 is assigned to x and the switch exits.

If x were 3, 100 is assigned to x.

In all other cases, 0 is assigned to x.

You will note that when the switch statement ends, x can only ever be 42, 33 or 0. Why not 100? Look again and you will see that case(3) has no break statement (just as case(1) has no break statement). So although 100 is assigned to x in case(3), the default case immediately assigns 0 to x.

This is not a particularly useful switch statement, but it serves to demonstrate how code flows through a switch statement, and how forgetting to put a break statement at the end of a case can cause unexpected errors to creep into your code.

While you could emulate the same results using a series of if, else if and else statements, the benefit of a switch statement is that it will generally simplify the complexity of your code, because there is only one expression to evaluate, rather than a series of boolean expressions. Your compiler will generate machine code just as if you'd used a series of if statements, but the switch statement gives a greater sense of structure to the statement, and gives a greater degree of control over which section(s) of code will execute for any given evaluation. And just as ifs can be nested within other ifs, switch statements can be nested within other switch statements.

Note that all code in a switch statement must be reachable (that is, not bypassed by all execution paths). For instance the following contains both unreachable and undefined code:

switch(x)

{

x=42; //unreachable

case(100):

int y=42;

break;

default:

x=y; // y is undefined

}

The line x=42 cannot be reached because execution must either pass to either the case(100) or the default label. Similarly, the x=y line is invalid because y is defined within the case(100) label, which is not reachable from within the default label due to the break statement.

User Avatar

Wiki User

10y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the syntax and purpose of the switch statement in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is the deffernce of the switch statement and the if statement in c plus plus?

If statement is single selection statement,whereas the switch statement is multiple selective.


Can you write plus at end of c statement?

No. That would be a syntax error. Only a right semicolon (;) can go at the end of a statement.


What is the function of visual c plus plus switch condition?

The switch / case statement.


Can you see a programme of if syntax in c plus plus?

My self Dhilib... it is a simple query. if statement is a basic control statement. mostly it used in all the languages. also in c plus plus syntax: if(test condition) { true statements; } else { false-statements; } Example: void main() { int a,b; a=54; b=65; if(a>b) { cout<<" a value is big"; } else { cout<<"b value is big"; } }


Is the syntax between c and c plus plus different?

Yes


X2 plus 26x plus?

(x2 + 26x +) : syntax error.x2 + 26x


How do you remove syntax error on Casio fx-85GT PLUS Calculator?

If you have a syntax error on a Casio fx-85GT Plus calculator, you can press the or key which will give you the location of the error and you can then correct it.


What is the syntax of printing a value in c plus plus?

std::cout<<42<<std::endl;


Which statement is not frequently used in C plus plus?

The goto statement.


What is syntax in c plus plus for declaring a variable?

type variable {[optional array size]} {= optional initializer};


What is the syntax of c plus plus?

The answer is really beyond the scope of this site. You would probably need to buy a textbook.


What is the main purpose of using comments in c plus plus?

The main advantage of the newer C++ comment syntax is that you only need to start the comment, and you can expect that it will last only until the end-of-line. With the older syntax, you needed to also stop the comment. any statement; /* old comment syntax */ any statement; // new comment syntax