0% found this document useful (0 votes)
12 views9 pages

Switch Statement

Uploaded by

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

Switch Statement

Uploaded by

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

The switch statement is a

control statement in C.

It allows a variable to be
tested for multiple values.
Switch
statement
Each value is called a case.

Used as an alternative to
multiple if-else statements
syntax

switch(expression) {
case value1:
// Statements
break;
case value2:
// Statements
break;
default:
// Statements
}
expression is evaluated once.

If it matches a case, the


corresponding block executes.

The break statement exits the


switch block.

The default case executes if


no match is found (optional).
int num = 2;
switch(num) {
case 1:
printf("One\n");
break;
case 2:
printf("Two\n");
Example break;
case 3:
printf("Three\n");
break;
default:
printf("Invalid Number\n");
}
✔ Simplifies multiple conditions.

Features of ✔ Executes only the matching


Switch case.
Statement ✔ Faster than multiple if-else
checks.
✔ Works with int and char data
types.
✔ Default case handles
unmatched values.
❌ Only integral values (int,
char) are allowed.
❌ No floating-point
comparisons (float,
Rules &
double).
Limitations ❌ Duplicate case values are
not allowed.
❌ No variable expressions
in case labels.
✅ Menu-driven
programs.
Use Cases
of Switch ✅ Handling keyboard
Statement inputs (A-Z, 0-9).
✅ Simple decision-
making scenarios.
Problem: Write a program that takes two
numbers and an operator (+, -, *, /) as
input. Use a switch statement to perform
the corresponding arithmetic operation
and display the result.
Problem: Write a program that takes an
integer (1-7) as input and prints the
corresponding day of the week using a
switch statement.

Problem: Write a C program that takes a


character input and checks if it is a vowel
(a, e, i, o, u) or a consonant using a
switch statement.
Problem: Create a menu-driven program where the user selects an
option:

Add
Subtract
Multiply
Divide
Use a switch statement to perform the selected operation.

Problem: Write a program that simulates a traffic light system. The


user enters a color (R, Y, G), and the program displays the meaning:

R: Stop
Y: Ready
G: Go

You might also like