0% found this document useful (0 votes)
3 views7 pages

7-Conditional Statements Switch Case

This document provides an overview of the switch statement in computer programming, including its syntax, usage, and examples. It explains the break and default keywords, along with their functions within a switch statement. Additionally, it includes exercises and tasks for practical application of the concepts learned.

Uploaded by

eishaahmed17
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)
3 views7 pages

7-Conditional Statements Switch Case

This document provides an overview of the switch statement in computer programming, including its syntax, usage, and examples. It explains the break and default keywords, along with their functions within a switch statement. Additionally, it includes exercises and tasks for practical application of the concepts learned.

Uploaded by

eishaahmed17
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/ 7

DEPARTMENT OF AVIONICS

ENGINEERING

SUBJECT : Computer Programming Lab

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:

 The switch expression is evaluated once


 The value of the expression is compared with the values of
each case
 If there is a match, the associated block of code is executed
 The break statement breaks out of the switch block and stops
the execution
 The default statement is optional, and specifies some code to
run if there is no case match
The example below uses the weekday number to calculate the weekday
name:
Example
int day = 4;

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;
}

// Outputs "Thursday" (day 4)

2 THE BREAK KEYWORD


When C reaches a break keyword, it breaks out of the switch block.
This will stop the execution of more code and case testing inside the block.
When a match is found, and the job is done, it's time for a
break. There is no need for more testing.
A break can save a lot of execution time because it "ignores" the execution
of all the rest of the code in the switch block.

3 THE DEFAULT KEYWORD


The default keyword specifies some code to run if there is no case match:

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");
}

// Outputs "Looking forward to the Weekend"


Note: The default keyword must be used as the last statement in the switch,
and it does not need a break.es
4 EXERCISE:
Insert the missing parts to complete the following switch statement:

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.

You might also like