You are on page 1of 5

Introduction to Computing Lab 10: Understand the Switch Cases

LAB # 10
To Understand The Decisions: SWITCH Case statements
Objective

 To understand Switch Case Statements in C++


 To learn statements for providing flexible jumping within program

Theory

Switch Case statements are additional for long IF declarations that relate a variable to a number
of "integral" values ("integral" values are simply values that can be expressed as an integer,
such as the value of a char or a ASCII Code). The basic syntax for using Switch Case is sketched
under. The value of the variable set into switch is compared to the value following each of the
cases, and when one value matches the value of the variable, the computer continues executing
the program from that point and onwards. If no value matches, it will jump to DEFAULT and
continues executing from that point.

Explanation:
A C++ switch statement acts as a routing device that tells the computer which line of code to
execute next. On reaching a switch statement, a program jumps to the line labeled with the value
corresponding to the value of integer-expression. For example, if variable has the value 2, the
program goes to the line that has a case 2: label. Also each label must be an integer constant
expression. Most often, labels are simple int or char constants, such as 1 or ‘q’ , or enumerators
(Enumerated data types are types that are well-defined with a set of custom identifiers, known as
enumerators, as possible values. Objects of these enumerated types can take any of these
enumerators as value). If value doesn’t match any of the labels, the program jumps to the line
labeled DEFAULT. The default label is optional. If you neglect it and there is no match, the
program jumps to the next statement following the switch, illustrated in figure below:

1
Introduction to Computing Lab 10: Understand the Switch Cases

Sample Syntax:
Below is the sample syntax of Switch – Case Statement:

switch ( <variable> ) {
case this-value:
{Code to execute if <variable> == this-value}
break;
case that-value:
{Code to execute if <variable> == that-value}
break;
...
default:
{Code to execute if <variable> does not equal the value following any of the cases}
break;
}

Sample Program:
The name of the month can be printed in a switch statement that used the value of "month" to
select the correct name. The switch statement required to select the month could be coded as:

...
Switch (month) {
case 1:
cout << "January ";
break;
case 2:
cout << "February ";
break;
...
...
case 12:
cout << "December ";
break;
}

Example Program – 1:
On the next page, it is the Example program – 1, it simple adds, subtracts, multiply and divide two
integers provided by user:

2
Introduction to Computing Lab 10: Understand the Switch Cases

#include <iostream>
using namespace std;
int main()
{
float a, b, result;
char operation;
cin >> a >> operation >> b; // USER must enter complete syntax example "5+4 or 4-3"
switch(operation)
{
case '+':
result = a + b;
break;
case '-':
result = a - b;
break;
case '*':
result = a * b;
break;
case '/':
result = a / b;
break;
default:
cout << "Invalid operation. Program terminated." << endl;
return -1;
}
cout <<"\nYour Answer is : ";
cout << result << endl;
return 0;
}

Example Program – 2:
Below is the example program – 2, using enum to define a set of related constants and then using
the constants in a switch statement. In general, cin doesn’t recognize enumerated types (it can’t
know how you will define them), so the program reads the choice as an int. When the switch
statement compares the int value to an enumerator case label, it promotes the enumerator to int.

// Switch.cpp -- using enumerator Data Type


#include <iostream>
// create named constants for 0 - 6
enum {red, orange, yellow, green, blue, violet, indigo};
int main()
{
using namespace std;
cout << "Enter color code (0-6): ";
int code;

3
Introduction to Computing Lab 10: Understand the Switch Cases

cin >> code;


while (code >= red && code <= indigo)
{
switch (code)
{
case red :
cout << "Her lips were red.\n"; break;
case orange :
cout << "Her hair was orange.\n"; break;
case yellow :
cout << "Her shoes were yellow.\n"; break;
case green :
cout << "Her nails were green.\n"; break;
case blue :
cout << "Her sweatsuit was blue.\n"; break;
case violet :
cout << "Her eyes were violet.\n"; break;
case indigo :
cout << "Her mood was indigo.\n"; break;
}
cout << "Enter color code (0-6): ";
cin >> code;
}
cout << "Bye\n";
return 0;
}
Output
Enter color code (0-6): 3
Her nails were green.
Enter color code (0-6): 5
Her eyes were violet.
Enter color code (0-6): 2
Her shoes were yellow.
Enter color code (0-6): 8
Bye

4
Introduction to Computing Lab 10: Understand the Switch Cases

Lab Task

Lab Task. 10.1) Write a program that inputs, user grade A to F and display its comments
according to grade entered.

Sample Output
Enter Your Grade: A
Excellent!
You entered Grade: A

Lab Task. 10.2) Write a program that ask user for which shape you want to calculate area
(Example Circle, square and triangle) and then take appropriate input from user to calculate.

Note: Attach with manual every above mentioned task.

You might also like