You are on page 1of 17

Programming Concepts and Constructs

(C++ Language)

Selection statements: Switch


Objectives

At the end of this presentation, you will be able to


• Write C++ programs using selection
statements - switch
Selection Statement-switch

• Definition: The switch statement is used when


you choose from a number of choices.
• The control statement that allows you to
choose from a number of choices is the switch
statement.
The syntax for switch statement is:

switch (<expression>){
case <constant1>:
<Statements1>;
break;
case <constant2>:
<Statements2>;
break;
...
...
default:
<Statementsn>; }
Execution:
• The expression following the keyword switch can either be an integer expression or a character
expression.
• An integer or a character constant follows the keyword case.
• Based on the value returned by the expression, the respective case is executed.
• The expression is first evaluated and this value is then matched against the constants in each
case.
• When a match is found, the statements following that particular case statement are executed. If
no match is found, the statements following the default case are executed.
Execution:
• The keyword break must be included in each case.
• A break statement will enable you to skip all the
cases following the current case and
• Transfer the control outside the switch statement.
• The break statement need not be included in the
default case.
• After executing the statements in the default case,
the control exits the switch statement
automatically.
#include <iostream.h> Program to illustrate the working of a
void main() { switch statement
int x;
cout<<"\n Enter the value for X (1,2 or 3) : ";
cin>>x;
switch (x) {
case 1:
cout<<" You have entered One ";
break;
case 2:
cout<<" You have entered Two ";
break;
case 3:
cout<<" You have entered Three ";
break;
default:
cout<<" Wrong Entry “; }
}
Sample Output :

Output 1:
Enter the value for X (1,2 or 3) : 1
You have entered One
Output 2:
Enter the value for X (1,2 or 3) : 5
Wrong Entry
Summary

In this presentation, you learnt the following:


• Selection statements are used to evaluate
expression and direct the execution of the
program, depending on the result of the
evaluation.
Activity 4.4.4 (a)

What will be the output of the following code?


int a = 1;
switch(a) {
case 1:
cout<<"\n Case 1 ";
break;
case 2:
cout<<"\n Case 2 ";
break;
case 3:
cout<<"\n Case 3 ";
break;
default:
cout<<"\n No match found";
}
Activity 4.4.4 (b)

What will be the output of the following code?


int a = 1;
switch(a)
{
case 1:
cout<<"\n Case 1 ";
case 2:
cout<<"\n Case 2 ";
case 3:
cout<<"\n Case 3 ";
default:
cout<<"\n No match found";
}
Exercise 1:
Write a program to accept a number that represents the
day of the week and display the corresponding day.
(*use switch..case statement for selection)
For example:
1 – Sunday
2 – Monday
3 – Tuesday
4 – Wednesday
5 – Thursday
6 – Friday
7 – Saturday
Exercise 2:
Using the same question in Exercise 1, convert
switch..case statement to if..else statement.
Exercise 3:

  Write a program using to accept a character


from the user. (*use if..else statement for
selection)

If the input is V or v, display the text Violet.


If the input is B or b, display the text Blue.
If the input is R or r, display the text Red.
For any other value, display the text Invalid
input.
Exercise 4:
Using the same question in Exercise 3, convert
if..else statement to switch..case statement.
Exercise 5:
Convert this statement into nested if. Write a full program.

int balance = 500;


int amount;
char code;
 
cout <<"enter your transaction code, d - deposit, w - withdrawal: \n";
cin >> code;
 
cout << "enter your amount: \n";
cin >> amount;
 
switch (code)
{
case 'd':
balance = balance + amount;
cout << "your balance “<< balance;
break;
case 'w':
balance = balance - amount;
cout << "your balance “<< balance;
break;
default:
cout << " code not allowed!\n";
}
Exercise 6:
Convert this statement into switch..case. Write a full program.

char grade;
 
if ( grade == 'A' || grade == 'a' )
cout<<" Your get grade " <<grade<<" Congratulation";
else if ( grade == 'B' || grade == 'b' )
cout<<" Your get grade " <<grade<<" Good";
else if ( grade == 'C' || grade == 'c' )
cout<<" Your get grade " <<grade<<" Need more exercise";
else if ( grade == 'D' || grade == 'd' )
cout<<" Your get grade " <<grade<<" Need improvement";
else if ( grade == 'F' || grade == 'f' )
cout<<" Your get grade " <<grade<<" Try again ";
else
{
cout<< "Incorrect letter grade entered." ;
cout<< " Enter a new grade.\n" ;
}

You might also like