switch statement in C | Working of switch statement in C

You must first complete Simple if statement in C language before viewing this Lesson

switch statement in C | Working of switch statement in C

It is a multi-branch selection statement that works with a variable or expression whose value is compared with a list of values known as cases of switch statement.

If  value of expression/variable matches with any of the case value, the block of statements associated with that case will be executed.

The expression/variable can be of short, long, int and char data type. The syntax of switch statement is:

switch(Expression)
{
case val1:
                        Block1;
case val2:
                        Block2;
case val3:
                        Block3;
:
:
default:
                        BlockN;    
}
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.

val1,val2,val3 are values that are compared expression or variable taken within switch statement. These are constant values or expressions.

If the value of expression matches with val1, Block1  gets executed, If value of expression matches with val2, Block2 will execute, if the value of expression matches with val3, Block3 will execute and so on. If value of expression doesn’t match with any of the case values, default case will execute. So BlockN will execute.

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.

Program to demonstrate the use of switch statement in C.

#include<stdio.h>
int main()
{
int day;
printf("\nEnter  value between 1 and 7:-");
scanf("%d",&day);
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, variable day has been entered  7. Value of day is compared with all case values defined within the switch(day) statement. As value of day matches with case 7:  So the statement printf(“Saturday”) would be executed.



DIFFERENCE BETWEEN IF AND SWITCH STATEMENT IN C

IF STATEMENT SWITCH STATEMENT
1. If statement works with  a relational or logical expression. 1. It works with an integer or character expression or variable .
2. If statement is categorized into four types. 2. It has no types.
3. There can be multiple Conditions in if statement. 3. There can be multiple cases.
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;
}
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.


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