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.
std::cout<<42<<std::endl;
The goto statement.
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
if (condition) statement else statement;
No. Java takes some ideas from C++, so there are certain similarities, especially in the basic syntax. But it is a different language.
If statement is single selection statement,whereas the switch statement is multiple selective.
No. That would be a syntax error. Only a right semicolon (;) can go at the end of a statement.
The switch / case statement.
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"; } }
Yes
(x2 + 26x +) : syntax error.x2 + 26x
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.
std::cout<<42<<std::endl;
The goto statement.
type variable {[optional array size]} {= optional initializer};
The answer is really beyond the scope of this site. You would probably need to buy a textbook.
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