C++ Programming
Lecture 7
Control Structure I
(Selection) – Part II
The Hashemite University
Computer Engineering
Department
(Adapted from the textbook slides)
Outline
Introduction.
switch statement.
Examples.
The Hashemite University
2
Introduction
In this lecture we will study the last
control structure in C++.
This control structure is associated
with selection.
It is used when there are multiple
selection choices, i.e. more than 2.
It can be used instead of nested
if/else structure.
The Hashemite University
3
The switch MultipleSelection Structure
switch
switch (variable or
expression)
Useful when variable or
{
expression is tested for multiplecase value1: //do
something;
values.
break;
case value2: //do
Consists of a series of case
labels and an optional default something;
break;
case.
case value3: //do
Used instead of nested if/else something;
break;
statements to make the code case value4: //do
something;
more readable and easier to
break;
trace.
default: // do something
The Hashemite University
break;
4
Flowchart Representation
of switch
case a
true
case a action(s)
break
case b action(s)
break
case z action(s)
break
false
case b
true
false
.
.
.
case z
true
false
default action(s)
The Hashemite University
5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Fig. 2.22: fig02_22.cpp
// Counting letter grades
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
char grade,
aCount =
bCount =
cCount =
dCount =
fCount =
0,
0,
0,
0,
0;
// one grade
// number of A's
// number of B's
// number of C's
// number of D's
// number of F's
cout << "Enter the letter grades." << endl
<< "Enter the Z character to end input." << endl;
cin >> grade;
while ( ( grade ) != ‘Z’ ) {
switch ( grade ) {
Notice how the case statement is used
// switch nested in while
case 'A': // grade was uppercase A
case 'a': // or lowercase a
++aCount;
break; // necessary to exit switch
case 'B': // grade was uppercase B
case 'b': // or lowercase b
++bCount;
break;
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
case 'C': // grade was uppercase C
case 'c': // or lowercase c
++cCount;
break;
case 'D': // grade was uppercase D
case 'd': // or lowercase d
++dCount;
break;
case 'F': // grade was uppercase F
case 'f': // or lowercase f
++fCount;
break;
default:
cout <<
<<
break;
// catch all other characters
"Incorrect letter grade entered."
" Enter a new grade." << endl;
// optional
}
}
cout <<
<<
<<
<<
<<
<<
"\n\nTotals for each letter grade are:"
"\nA: " << aCount
"\nB: " << bCount
"\nC: " << cCount
"\nD: " << dCount
"\nF: " << fCount << endl;
return 0;
Enter the letter grades.
Enter the EOF character to end input.
a
B
c
C
A
d
f
C
E
Incorrect letter grade entered. Enter a new grade.
D
A
B
Z
Totals for each letter grade are:
A: 3
B: 2
C: 3
D: 2
F: 1
Program Output
Notes I
default statement is optional in switch.
In the previous example variable grade is called the
controlling expression.
If no break statement is included, all case statements will be
implemented once a match has been found.
Logical errors:
Forgetting break statement.
Forgetting white space between case and the value to test
against it( e.g. case3:). This will create a label.
default and case statements can be placed in any order
inside the switch structure.
No braces are required around multiple statements for the
same case structure in switch. break determine the end of
the case code block.
Identical case labels in switch is a syntax error.
The Hashemite University
9
Break example
#include<iostream>
using namespace std;
int main()
{
int grade=0;
cin>>grade;
switch (grade+10)
{
default: cout<<"no matching
found";
case 50: cout<<"50";
break;
case 40: cout<<"40";
break;
case 60: cout<<"60";
case 70: cout<<"70";
case 80: cout<<"80";
}
return 0;
}
Placing case 80: cout<<”hi”;
is syntax error (never place
similar cases in one program)
If you enter 40 , the program will display 50
If you enter 30 , the program
40
If you enter 50 , the program
607080
If you enter 60 , the program
7080
If you enter 10 , the program
no matching found50
will display
will display
will display
will display
Remember that writing the switch or the
break like this Switch, Break will give a
syntax error
The Hashemite University
10
Notes II
switch is used for testing constant integral
expressions only, i.e. float, arrays, strings are
not allowed. Only integer values or single
character values. These values can be the
result of an expression, variable, or a constant
value. (x + 5) for example is allowed.
For case statements only constants integer
values are allowed (either integer or single
characters). No expressions, float/double
values, and variables are allowed.
What to do when you want to test double
values??? (nested if/else statements).
The Hashemite University
11
Additional Notes
This lecture covers the following
material from the textbook:
Fourth Edition:
Chapter 2: Section 2.16
The Hashemite University
12