0% found this document useful (0 votes)
26 views

Switch in CPS

Switch statements are used to select one of many code blocks to be executed. It tests for equality between an expression and multiple case values. The syntax includes the switch expression, cases with associated code blocks and an optional default. When a match occurs, the corresponding code block executes until a break is reached. Examples demonstrate using switch to output grades and days of the week based on user input.

Uploaded by

Melwin
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Switch in CPS

Switch statements are used to select one of many code blocks to be executed. It tests for equality between an expression and multiple case values. The syntax includes the switch expression, cases with associated code blocks and an optional default. When a match occurs, the corresponding code block executes until a break is reached. Examples demonstrate using switch to output grades and days of the week based on user input.

Uploaded by

Melwin
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 5

C Programming

By:
Prof. Melwin D souza
HOD, CSE Dept.
MOODLAKATTE INSTITUTE OF TECHNOLOGY, KUNDAPURA
switch statement
• Used to solve multiple option (menu)
• One value is associated with each option/case

Syntax: Working of Switch


switch (expression) First, the expression within switch is evaluated
{ The value of an expression is compared with all the
case value-1 : statements; case values.
break; Example: The value of an expression is compared with
case value-2 : statements; the case value-1. If it matches then the statements
break;
case value-3 : statements; associated with that case are executed. If not then the
break; case value-2 is compared, if it matches then the
: associated statements are executed and so on
: The “default” statements are executed when no
case value-n : statements; match is found.
break;
A default is optional
default: statements;
break; The break statement makes the control to come out
} of the switch. The break indicates end of a particular
case
Programming Examples
c program to display grade

#include <stdio.h> case ‘B’: printf(“Good”);


void main() break;
{ case ‘C’: printf(“Fair”);
char grade; break;
printf(“Enter the grade:\n”); case ‘F’: printf(“Fail”);
scanf(“%c”, &grade); break;
switch(grade) default : printf(“Invalid grade”);
{ }
case ‘O’: printf(“Outstanding”);
break;
} Enter the grade:
A
case ‘A’: printf(“Excellent”); Excellent
break;
Programming Examples
c program to display the day of week

#include <stdio.h> case 4: printf(“Wednseday”);


void main() break;
{
case 5: printf(“Thursday”);
int day;
break;
printf(“Enter Day number:\n”);
scanf(“%d”, &day); case 6: printf(“Friday”);
switch(day) break;
{ case 7: printf(“Saturday”);
case 1: printf(“Sunday”); break;
break;
default : printf(“invalid input”);
case 2: printf(“Monday”);
break;
}
Enter Day number:
case 3: printf(“Tuesday”); } 3
break;
Tuesday
example 2
c program to to display the day of week

#include <stdio.h> case 4: printf(“Wednseday”);


void main() break;
{
case 5: printf(“Thursday”);
int i=1, sum=0;
break;
printf(“Enter Day number:\n”);
scanf(“%d”, &day); case 6: printf(“Friday”);
switch(day) break;
{ case 7: printf(“Saturday”);
case 1: printf(“Sunday”); break;
break;
default : printf(“invalid input”);
case 2: printf(“Monday”);
break;
}
Enter Day number:
case 3: printf(“Tuesday”); } 3
break;
Tuesday

You might also like