0% found this document useful (0 votes)
35 views20 pages

C++ Switch Statement Explained

The document provides an introduction to the switch statement in programming, explaining its purpose as a substitute for nested if-else structures when multiple choices are present. It details the syntax, usage of break and default statements, and includes examples of C++ programs that utilize the switch statement for various applications, such as determining even or odd numbers and calculating tax based on salary. Additionally, it briefly mentions the goto statement as an unconditional branch of execution.

Uploaded by

h8327917
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)
35 views20 pages

C++ Switch Statement Explained

The document provides an introduction to the switch statement in programming, explaining its purpose as a substitute for nested if-else structures when multiple choices are present. It details the syntax, usage of break and default statements, and includes examples of C++ programs that utilize the switch statement for various applications, such as determining even or odd numbers and calculating tax based on salary. Additionally, it briefly mentions the goto statement as an unconditional branch of execution.

Uploaded by

h8327917
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

Introduction to

Programming Lecture Switch


Statement

Muhammad Salman
Summary
 Switch Statement

 First Program

 Break Statement

 Program

 Default statement
 goto Statment
Switch statment

 It is the statement that allows a variable to be tested against a list of values


for equality. The value in the switch is termed as a case, and hence the
variable being switched on is checked against the case.

 Switch statement is used as a substitute for a “nested if-else” statement. It is


used when multiple choice is given and one choice is to be made.

 Nested if-else structure becomes complicated in multiple choice. The switch


statement is used in such cases.

 The expression in the switch statement of C++ is valid only if it results in a


constant value.
 The switch statement evaluates an expression and returns a value.
 The returned value is compared with the constant values given in the case.
The switch statement flow digram
Syntax
switch (Expression)
{
case 1 :
statement 1 ;
case 2 :
statement 2 ;
case N :
statement n ;
default:
statement d ;

}
C++ Switch Statement Program

 It is the statement that


allows a variable to be
tested against a list of
values for equality
(A,B,C,D,F). The value in the
switch is termed as a case,
and hence the variable being
switched on is checked
against the case.
C++ Switch Statement Program to match the case

Input:

cin >> x; reads an integer value from the user and


stores it in x.

Switch Statement:

The switch statement evaluates the value of x.

If x is 0, the case 0: block is executed, and it prints


"You entered 0".

If x is 1, the case 1: block is executed,


and it prints "You entered 1".

If x is 2, the case 2: block is executed,


and it prints "You entered 2".
Break statement
 The break statement interrupts the flow of control.

 We have seen in the last example that even the true case was found but the
flow of control went through all the statements.

 We want only the true case should be executed.

 For this purpose, we use the “break statement”

 Write it after the case, thus when a true case is found execute the statement
then the break statement interrupts the flow of control, and control jumps out
of the switch statement.

 If it is not used, then the statement of other cases that come after the
matching case will also execute.
Break statement

Yes !!!‘A’ is different


from ‘a’
case ‘A’ :
case ‘a’ :
cout <<“Excellent” <<
endl;

Refer to the next


slide
Break statement
C++ Switch Statement Program to find an even number
n % 2 Expression:
 The case labels must match the possible
 The expression n % 2 calculates the
results of n % 2, which are 0 and 1.
remainder when n is divided by 2.

 If n is even, n % 2 will be 0. case 0:

 If n % 2 is 0, the program executes the


 If n is odd, n % 2 will be 1.
block under case 0 and prints that n is
even.
switch Statement:
case 1:
The switch statement evaluates the
result of n % 2.  If n % 2 is 1, the program executes the
block under case 1 and prints that n is
case 0: corresponds to even
odd.
numbers.

case 1: corresponds to odd numbers.


case 2:
case 3:
Default case
 The default case in a switch statement acts similarly to an else statement in
an if-else block.
 It is executed when none of the other cases match the value of the
expression being evaluated.

 Comparison:
 The else block is executed when none of the proceedings if or else if
conditions is true
Default case
Basic Arithmetic operation using switch statement
Basic Arithmetic operation using switch statement
Salary calculation using switch statement
 If salary is less than 60000 no tax
will deduct.
 If salary slab is 60000 -119000 then
5 % tax will deduct ,
 if salary slab is 120000 and above
15 % tax will deduct
goto statment
 The goto is an unconditional branch of execution.

 goto statement is used to jump the control anywhere (back and forth )
in the program.
goto statment
Thanks

You might also like