switch statement in C | Working of switch statement in C

Preview
You must first complete if else if ladder statement in c language before viewing this Lesson

 Switch statement

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(Expression)
{
case value1:
                        Statement-Block1;
case value2:
                        Statement-Block2;
case value3:
                        Statement-Block3;
:
:
default:
                        Statement-BlockN;    
}
Statement-X

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.

StatementBlock1, StatementBlock2, StatementBlock3,…. StatementBlockN 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.
#include<stdio.h>
int main()
{
int d;
printf(“\nEnter  value between 1 and 7:-“);
scanf(“%d”,&d);
switch(d)
{
case 1:
printf(“Sunday”);
break;case 2:
printf(“Monday”);
break;case 3:
printf(“Tuesday”);
break;

case 4:
printf(“Wednesday”);
break;

case 5:
printf(“Thursday”);
break;

case 6:
printf(“Friday”);
break;

case 7:
printf(“Saturday”);
break;

default:
printf(“Wrong choice”);
break;
}
return(0);
}

Output
Enter a value between 1 and 7:-7
Saturday
Description

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 STATEMENTSWITCH 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:

  • Simple if
  • If else
  • Nested if
  • If else if ladder
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:
if(Condition)
{
Statement-Block;
}
Statement-X;
5. Syntax of switch statement:
switch(Expression)
{
case value1:
Block1;
case value2:
Block1;
case value3:
Block1;
:
:
:
default:
BlockN;
}
6.
#include<stdio.h>
int main()
{
int a=10;
clrscr();
if(a>0)
printf(“number is +ve”);
printf(“\nBye”);
return(0);
}
6.
#include<stdio.h>
int main()
{
char choice;
printf(“\na) Case A”);
printf(“\nb) Case B”);
printf(“\nEnter your choice:-“);
scanf(“%c”,&choice);
switch(choice)
{
case ‘a’:
case ‘A’:
printf(“\nCase A selected”);
break;case ‘b’:
case ‘B’:
printf(“\nCase B selected”);
break;default:
printf(“Wrong choice”);
}
return(0);
}
8. There are no cases or case values in if statement8. There are various cases in switch statement and all case values must be constant values.


Lesson tags: conditional statement in c, difference between if and switch, selection statement in c, switch statement in c, switch statement of c
Back to: C Programming Language