7-Conditional Statements Switch Case
7-Conditional Statements Switch Case
ENGINEERING
LAB NO : 07
Conditional Statements –
TITLE :
Switch Case
SUBMITTED TO : Ms. Maha Intakhab Alam
SUBMITTED BY :
BATCH : Avionics 10
SECTION :
Marks Obtained :
Remarks:
DEADLINE:
DATE OF SUBMISSION:
Table of Contents
1 Switch Statement......................................................................................3
Syntax..........................................................................................................3
Example........................................................................................................4
2 The break Keyword....................................................................................4
3 The default Keyword..................................................................................5
Example........................................................................................................5
4 Exercise:....................................................................................................6
5 TASKS……………………………………………………………………………………..
………………….7
1 SWITCH STATEMENT
Instead of writing many if..else statements, you can use
the switch statement.
The switch statement selects one of many code blocks to be executed:
Syntax
switch (expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
This is how it works:
switch (day) {
case 1:
printf("Monday");
break;
case 2:
printf("Tuesday");
break;
case 3:
printf("Wednesday");
break;
case 4:
printf("Thursday");
break;
case 5:
printf("Friday");
break;
case 6:
printf("Saturday");
break;
case 7:
printf("Sunday");
break;
}
Example
int day = 4;
switch (day) {
case 6:
printf("Today is Saturday");
break;
case 7:
printf("Today is Sunday");
break;
default:
printf("Looking forward to the Weekend");
}
int day = 2;
switch () {
1:
printf("Monday");
;
2:
printf("Sunday");
;
}
Submit Answer »
TASKS
i. Grade Calculator: Create a program that takes a student's score as
input and converts it into a letter grade (A, B, C, D, or F) using the
following grading scale:
A: 90-100
B: 80-89
C: 70-79
D: 60-69
F: Below 60
ii. Calculator Program: Write a simple calculator program that takes two
numbers and an operator (+, -, *, /) as input and performs the
corresponding arithmetic operation.
iii. Month Name to Number: Write a program that takes the name of a
month as input and returns the corresponding number (1 for January, 2
for February, etc.).
iv. Simple ATM Machine: Develop a simple ATM machine program that
offers options like withdraw, deposit, and check balance.
v. Day of the Week: Create a program that takes a number from 1 to 7 as
input and outputs the corresponding day of the week (1 for Sunday, 2
for Monday, etc.).
vi. Simple Menu Driven Program: Write a program that displays a menu to
the user with options like addition, subtraction, multiplication, and
division. Depending on the user's choice, perform the corresponding
operation.
vii. Temperature Converter: Build a program that converts temperature
units (Celsius to Fahrenheit or Fahrenheit to Celsius) based on user
input.
viii. Simple Game Menu: Develop a simple text-based game menu with
options like start game, load game, save game, and exit.