You are on page 1of 12

30-Nov-20

INTRODUCTION TO PROGRAMMING(CS-1102)

Lecture# 05
Course Instructor: Anam Naseer

Department of CS & IT, University of Poonch Rawalakot Azad Kashmir

C++ How to Program by Paul Deitel & Harvey Deitel, Eighth Edition 1
30-Nov-20

Switch Statement

 What is Switch Statement?


 Syntax
 Examples

C++ How to Program by Paul Deitel & Harvey 2


Deitel, Eighth Edition
Switch Statement
• Switch case statements are a substitute for long “if
statements” that compare a variable to several integral
values
 The switch statement is convenient for handling
multiple branches based on the value of one decision
variable
− The program looks at the value of the decision
variable
− The program jumps directly to the matching case
label
− The statements following the case label are
executed
29-Nov-20
C++ How to Program by Paul Deitel & Harvey Deitel, Eighth Edition 3
Syntax

• switch ( decision variable )


• {
• case value1 :
• // Statements to execute if variable equals value1
• break;
• case value2:
• // Statements to execute if variable equals value2
• break;
• ...
• default:
• // Statements to execute if variable not equal to any value
• }
29-Nov-20
C++ How to Program by Paul Deitel & Harvey Deitel, Eighth Edition 4
Syntax Description
• The expression provided in the switch should result in a constant
value otherwise it would not be valid.

• Duplicate case values are not allowed.

• The default statement is optional. Even if the switch case


statement do not have a default statement,
it would run without any problem.

• The break statement is used inside the switch to terminate a


statement sequence. When a break statement is reached, the
switch terminates, and the flow of control jumps to the next line
following the switch statement.

• The break statement is optional. If omitted, execution will continue


on into the next case. The flow of control will fall through to
subsequent cases until a break is reached.
29-Nov-20
C++ How to Program by Paul Deitel & Harvey Deitel, Eighth Edition 5
29-Nov-20

Flowchart

C++ How to Program by Paul Deitel & Harvey Deitel, Eighth Edition 6
Example 1 29-Nov-20

C++ How to Program by Paul Deitel & Harvey Deitel, Eighth Edition 7
Example 2

To print week day name

8
Example 3

To find maximum

9
Example 4 To check vowel or consonant

C++ How to Program by Paul Deitel & Harvey Deitel, Eighth Edition 10
Practice Questions
Solve all of these questions using both switch statement and if-else statement

Q#01:
Write a program that accepts the code number as an input and display the correct disk drive
manufactures as follows:
• Code Disk drive manufacture
• 1 Western Digital
• 2 3G Corporation
• 3 Maxell Corporation
• 4 Sony Corporation
• 5 Verbatim Corporation
Q#02:
• Write a program that uses the following categories of movies:
• A for Adventure movies
• C for Comedy movies
• F for Family movies
• H for Horror movies
• S for Science fiction movies
• The program inputs code for movie type and displays its category. For example if the user enter H,
it displays “Horror movies”. The program should also display a menu of movie categories.
Q#03
• Write A Program To Check Whether A Number Is Positive Negative Or Zero

C++ How to Program by Paul Deitel & Harvey Deitel, Eighth Edition 11
29-Nov-20
C++ How to Program by Paul Deitel & Harvey Deitel, Eighth Edition 12

You might also like