Switch is an alternative to using a long sequence of if...else if... statements where each conditional expression is essentially the same, evaluating to the same data type but with different values.
For instance:
if(x==1) statement_1;
else if(x==2) statement_2;
else if(x==3) statement_3;
else statement_4;
The above can also be written:
switch(x)
{
case(1): statement_1; break;
case(2): statement_2; break;
case(3): statement_3; break;
default: statement_4;
}
Most would regard the second version as more readable as there's only one conditional expression to evaluate.
A switch statement evaluates an expression and immediately jumps to the case label that matches the evaluation of that expression. Execution then continues from that point on, passing through all subsequent case labels to the end of the switch statement unless a break, return or goto statement is encountered, at which point the switch statement terminates. Case labels must be unique and must represent a single evaluation of the expression, but they can be placed in any order desired. A default case can also be given, but it must be the last case. Switch statements may also be nested within case labels.
The switch statement is a complex structure, but one that gives greater control over the flow of execution more easily than can otherwise be achieved with a series of if statements. However, simple switch statements are often interchangeable with simple if statements so whichever you use will usually be determined by which is more readable in your code.
The switch statement allows the programmer to evaluate an expression and to immediately jump to a specific case label, much like a goto label. Execution continues from that point on, falling through any subsequent case labels, unless a break, return or goto statement is encountered. An optional default case can be used as a catch-all when none of the case labels match the evaluation. The following is an example:
switch(x)
{
case(0):
function_1(x);
break;
case(1):
case(2):
function_2(x);
default:
function_3(x);
}
Note how the code flow is dependant upon the value of x. If x is zero, only function_1() is invoked, because the break statement terminates the switch statement. But if x is either 1 or 2, then function_2() and function_3() are invoked because there is no break statement. And if x is any other value besides 0, 1 or 2, then only function_3() is invoked.
As you can see, the switch statement gives a high degree of control over the code flow, much more easily than would be possible with a series of if/else-if statements. Without a switch statement, the above would need to be written as follows:
if( !x )
function_1(x);
else if(x==1 x==2)
{
function_2(x);
function_3(x);
}
else
function_3(x);
As you can, the switch statement is arguably much easier to read, but it is also more efficient. For instance, if x were neither 0, 1 or 2, execution would immediately jump to the default label. But with the if/else-if code, x must be evaluated three times before the final else clause is reached.
Here is an example:
#include <stdio.h>
int main();
{
int 1or2;
printf("1 or 2: ");
scanf("%d", &1or2);
switch (1or2)
{
case 1:
printf("You picked 1");
break; /*break; is used to break off the switch statement*/
case 2:
printf("You picked 2");
default: printf("You didn't pick 1 or 2...");
}
return 0;
}
Here is the program running:
o
/\
|
/\ /*Looks bad*/
Here is the real program running:
choice 1:
{
1 or 2: 1
You picked 1
}
choice 2:
{
1 or 2: 2
You picked 2
}
choice 3:
{
1 or 2: 3
You didn't pick 1 or 2...
}
There are two programming languages which use a C switch statement. The two languages are C and C++, hence the name C switch statement. There may be more, but those are the most obvious ones
The goto statement.
Yes, you can call a function from within a switch statement in C. switch (i) { case 0: function1(i); break; case 1: function2(i); break; default: function3(i); break; } When the function returns, you will still be in the switch statement.
...a function call.
if (condition) statement else statement;
If statement is single selection statement,whereas the switch statement is multiple selective.
The switch / case statement.
There are two programming languages which use a C switch statement. The two languages are C and C++, hence the name C switch statement. There may be more, but those are the most obvious ones
The goto statement.
Control statements are statements that alter the flow of execution according to the evaluation of an expression (the condition). The C++ control statements are ifstatements, switch statements and the tertiary conditional operator, ?:.
An object in C++ is an instance of a C++ class.
Yes, you can call a function from within a switch statement in C. switch (i) { case 0: function1(i); break; case 1: function2(i); break; default: function3(i); break; } When the function returns, you will still be in the switch statement.
The basic control structure in C++ is the if statement.
The break statement exits control of the innermost for, while or do-while loop, or switch statement.
...a function call.
if (condition) statement else statement;
if while switch