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

Switch Case Definition

The document discusses the switch case statement in C programming. It defines switch case as a type of selection mechanism that allows code from multiple alternatives to execute based on the value of an expression. It provides rules for switch case statements, including that they work with integer and enum data types, each case ends with a break statement, and there can only be one default case. Examples are given showing how to use switch case to print the name of a day or month based on a number input.

Uploaded by

cm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
145 views

Switch Case Definition

The document discusses the switch case statement in C programming. It defines switch case as a type of selection mechanism that allows code from multiple alternatives to execute based on the value of an expression. It provides rules for switch case statements, including that they work with integer and enum data types, each case ends with a break statement, and there can only be one default case. Examples are given showing how to use switch case to print the name of a day or month based on a number input.

Uploaded by

cm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Switch Case Definition

In C programming language, the switch statement is a type of selection mechanism


used to allow block code among many alternatives. Simply, It changes the control
flow of program execution via multiple blocks.

Flow diagram of switch case

1
How to switch and case statements work?

In switch statement when you are passed and integral argument then it searches the
case constant-expression within its body. If the integral value matches with the
case statement then control directly jump to the case statement and execute the
code until the end of the body or break statement, break statement transfer the
control out of the body.

In switch case, we can use the break statement to end the execution of the code for
the particular case. If we forgot to put the break statement after each case, the
program continues to the next case until not getting a break or end of the code.

Switch Statement  Rules


1. A switch works with the char and int data types.
2. It also works with enum types.
3. Switch expression/variable data type and case data type are should be
matched.
4. A switch block has many numbers of case statements Each case ends
with a colon.
5. Each case ends with a break statement. Else all case blocks will execute
until a break statement is reached.
6. The switch exists When a break statement is reached, 
7. A switch block has only one number of default case statements, It should
end of the switch.
8. The default case block executed when none of the cases is true.
9. No break is needed in the default case.

2
Switch Statement  Usage :
1. We can use switch statements alternative for an if..else ladder.
2. The switch statement is often faster than nested if...else Ladder.
3. Switch statement syntax is well structured and easy to understand.

Why do we need a Switch case?

There is one potential problem with the if-else statement which is the
complexity of the program increases whenever the number of alternative path
increases. If you use multiple if-else constructs in the program, a program might
become difficult to read and comprehend. Sometimes it may even confuse the
developer who himself wrote the program.

C programming switch case Examples/Programs :

1. Write a C program to print day of week name using switch case.


2. Write a C program print total number of days in a month using switch
case.
3. Write a C program to check whether an alphabet is vowel or consonant
using switch case.
4. Write a C program to find maximum between two numbers using switch
case.
5. Write a C program to check whether a number is even or odd using switch
case.
6. Write a C program to check whether a number is positive, negative or zero
using switch case.
7. Write a C program to find roots of a quadratic equation using switch case.
8. Write a C program to create Simple Calculator using switch case

3
1. C program to read gender (M/F) and print corresponding gender
using switch.

#include <stdio.h>
int main()
{
char gender;
printf("Enter gender (M/m or F/f): ");
scanf("%c",&gender);
switch(gender)
{
case 'M':
case 'm':
printf("Male.");
break;
case 'F':
case 'f':
printf("Female.");
break;
default:
printf("Unspecified Gender.");
}
printf("\n");
return 0;
}

4
2. C program to find number of days in a month using switch case .

#include <stdio.h>

int main()

int month;

int days;

printf("Enter month: ");

scanf("%d",&month);

switch(month)

case 4:

case 6:

case 9:

case 11:

days=30;

break;

case 1:

case 3:

case 5:

case 7:

case 8:

case 10:

case 12:
5
days=31;

break;

case 2:

days=28;

break;

default:

days=0;

break;

if(days)

printf("Number of days in %d month is: %d\n",month,days);

else

printf("You have entered an invalid month!!!\n");

return 0;
}

6
3. C program to read weekday number and print weekday name using
switch.

int main()

int wDay;

printf("Enter weekday number (0-6): ");

scanf("%d",&wDay);

switch(wDay)

case 0:

printf("Sunday");

break;

case 1:

printf("Monday");

break;

case 2:

printf("Tuesday");

break;

case 3:

printf("Wednesday");

break;

case 4:

printf("Thursday");

7
break;

case 5:

printf("Friday");

break;

case 6:

printf("Saturday");

break;

default:

printf("Invalid weekday number.");

printf("\n");

return 0;

8
4. Write a program in C to accept a grade and display the equivalent
description.

Grade Description

E Excellent

V Very Good

G Good

A Average

F Fail

#include<stdio.h>

int main()

Char grd;

printf("Input the grade :");


scanf("%c", &grd);
switch(grd)
{
case 'E':
strcpy(notes, " Excellent");
break;
case 'V':
strcpy(notes, " Very Good");

9
break;
case 'G':
strcpy(notes, " Good ");
break;
case 'A':
strcpy(notes, " Average");
break;
case 'F':
strcpy(notes, " Fails");
break;
default :
strcpy(notes, "Invalid Grade Found. \n");
break;
}

10

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy