Switch statement is a multi-branch selection statement. Switch statement works with a variable or expression known as control variable or control expression.
Switch tests the value of control expression/variable with a list of values known as cases of switch statement.
When the value of control expression/variable matches with any of the case value, the block of statements associated with that case will be executed.
The control expression/variable can be of short, long, int and char data type. The syntax of switch statement is:
switch is a keyword to specify the beginning of switch statement.
Expression is any integer or character variable or expression on which switch works.
case is a keyword which is used to specify any case within switch statement
value1,value2,value3 are various case values which are compared with the value of expression or variable taken within parenthesis after switch statement. These values can be only constant values or expressions.
If the value of expression matches with value1, Statement-Block1 will execute, If value of expression matches with value2, Statement-Block2 will execute, if the value of expression matches with value3, Statement-Block3 will execute and so on. If value of expression doesn’t match with any of the case values, default case will execute. So Statement-BlockN will execute.
Statement–Block1, Statement–Block2, Statement–Block3,…. Statement–BlockN are sets of C language statements which will execute depending on value of expression on which switch works.
break is a jumping statement which is written after each case within switch statement. So that after execution of statements within a case, the control should jump out of switch statement.
If we don’t write break statement after each case, all the consecutive cases after the matching case will execute.
Statement-X may be a single statement or group of statements which will be executed in both the cases of condition to be true as well as false.
Switch statement can be represented with the help of a flowchart as
|Program to demonstrate the use of switch statement.|
printf(“\nEnter value between 1 and 7:-“);
|Enter a value between 1 and 7:-7|
In the above program, d has been entered 7.
Value of d is compared with all case values defined within the switch(d) statement.
As value of d matches with case 7:
So the statement printf(“Saturday”) would be executed.
DIFFERENCE BETWEEN IF AND SWITCH STATEMENT
|IF STATEMENT||SWITCH STATEMENT|
|1. If statement works with a relational or logical expression. Depending upon the value of the expression it further gets executed.||1. It works with an integer or character expression or variable which is further compared with various case values to execute a specific case.|
|2. If statement is categorized into four types as:||2. It has no sub types.|
|3. There can be multiple Conditions in if statement.||3. In a switch statement, there can be multiple cases whose values are compared with the expression used with switch statement.|
|4. If statement can work with any relational or logical operator.||4. It can only work with equality operator.|
|5. Syntax of if statement:|
|5. Syntax of switch statement:|
printf(“number is +ve”);
printf(“\na) Case A”);
printf(“\nb) Case B”);
printf(“\nEnter your choice:-“);
printf(“\nCase A selected”);
printf(“\nCase B selected”);
|8. There are no cases or case values in if statement||8. There are various cases in switch statement and all case values must be constant values.|