Module 1 & 2 Third

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 14

PRESENTATION TITLE

C Switch Statement
• The switch statement in C is an alternate to if-else-if ladder statement which
allows us to execute multiple operations for the different possibles values of a
single variable called switch variable.
• Here, We can define various statements in the multiple cases for the different
values of a single variable.

1
PRESENTATION TITLE

switch(expression){
case value1:
//code to be executed;
break; //optional
case value2:
//code to be executed;
break; //optional
......

default:
code to be executed if all cases are not matched;
}
2
PRESENTATION TITLE

Rules for switch statement in C language

1) The switch expression must be of an integer or character type.

2) The case value must be an integer or character constant.

3) The case value can be used only inside the switch statement.

4) The break statement in switch case is not must. It is optional. If there is no break statement found in
the case, all the cases will be executed present after the matched case. It is known as fall through the
state of C switch statement.
3
PRESENTATION TITLE
#include<stdio.h>
int main(){
int number=0;
printf("enter a number:");
scanf("%d",&number);
switch(number){
case 10:
printf("number is equals to 10");
break;
case 50:
printf("number is equal to 50");
break;
case 100:
printf("number is equal to 100");
break;
default:
printf("number is not equal to 10, 50 or 100");
}
return 0;
4 }
PRESENTATION TITLE

How switch Statement Work?


The working of the switch statement in C is as follows:
1.Step 1: The switch variable is evaluated.
2.Step 2: The evaluated value is matched against all the present cases.
3.Step 3A: If the matching case value is found, the associated code is executed.
4.Step 3B: If the matching code is not found, then the default case is executed if present.
5.Step 4A: If the break keyword is present in the case, then program control breaks out of the
switch statement.
6.Step 4B: If the break keyword is not present, then all the cases after the matching case are
executed.
7.Step 5: Statements after the switch statement are executed.
5 We can also understand the working of the switch statement in C using the flowchart.
PRESENTATION TITLE

Flowchart of Switch
Statement

6
PRESENTATION TITLE

Break in switch case

This keyword is used to stop the execution inside a switch block. It helps to terminate the

switch block and break out of it. When a break statement is reached, the switch

terminates, and the flow of control jumps to the next line following the switch statement.

The break statement is optional. If omitted, execution will continue on into the next

case. The flow of control will fall through to subsequent cases until a break is reached.

7
PRESENTATION TITLE // C Program to demonstrate the behaviour of switch case
// without break
#include <stdio.h>

int main()
{

int var = 2;
Output
// switch case without break
switch (var) { Case 2 is executed.
case 1:
printf("Case 1 is executed.\n"); Case 3 is executed.Case 4 is executed.
case 2:
printf("Case 2 is executed.\n");
case 3:
printf("Case 3 is executed.");
case 4:
printf("Case 4 is executed.");
}
8 return 0;
}
PRESENTATION TITLE

Default in switch case

The default keyword is used to specify the set of statements to execute if there is no case

match.

It is optional to use the default keyword in a switch case. Even if the switch case statement

does not have a default statement, it would run without any problem.

9
PRESENTATION TITLE

Important Points About Switch Case Statements

1. Switch expression should result in a constant value


If the expression provided in the switch statement does not result in a constant value, it would not be
valid. Some valid expressions for switch case will be,

//constant expressions allowed


switch(1+2+23)
switch(1*2+3%4)

// Variable expression are allowed provided


// they are assigned with fixed values
switch(a*b+c*d)
switch(a+b+c)
10
PRESENTATION TITLE

2. Expression value should be only of int or char type.


The switch statement can only evaluate the integer or character value. So the switch expression
should return the values of type int or char only.
3. Case Values must be Unique
In the C switch statement, duplicate case values are not allowed.
3. Nesting of switch Statements
Nesting of switch statements is allowed, which means you can have switch statements inside
another switch. However nested switch statements should be avoided as it makes the program more
complex and less readable.

11
// C program to print the day using switch
PRESENTATION TITLE #include <stdio.h>

// Driver Code case 5:


int main() printf("Thursday");
{ break;
int day = 2; case 6:
printf("Thursday");
printf("The day with number %d is ", day); break;
switch (day) { case 7:
case 1: printf("Thursday");
printf("Monday"); break;
break; default:
case 2: printf("Invalid Input");
printf("Tuesday"); break;
break; }
case 3: return 0;
printf("Wednesday"); }
break;
case 4:
printf("Thursday");
12 break;
PRESENTATION TITLE

Advantages of C switch Statement

1.Easier to read than if else if.

2.Easier to debug and maintain for a large number of conditions.

3.Faster execution speed.

Disadvantages of C switch Statement

4.Switch case can only evaluate int or char type.

5.No support for logical expressions.

6.Have to keep in mind to add a break in every case.


13
THANK YOU

You might also like