Selection Constructs

CSCE 110
PROGRAMMING FUNDAMENTALS
WITH
C++
Prof. Amr Goneid
AUC
Part 3. Selection Constructs
Prof. amr Goneid, AUC
1
Selection Constructs
Prof. amr Goneid, AUC
2
Selection Constructs
 Basic C++ Constructs
 Sequential Constructs
 Selection with if Statement
 The Ternary Operator
 The switch Construct
Prof. amr Goneid, AUC
3
1. Basic C++ Constructs
 Sequential construct : statements




performed in succession, single or as a block
Selection construct : implemented with ifelse and switch statements
Repetition construct : implemented with for
, while , and do- while loops
Functional construct : a program is
decomposed into functions (Modules)
Object-oriented : a program is decomposed
into objects.
Prof. amr Goneid, AUC
4
2. Sequential Constructs
 Simple Statement:
c = a + b;
// Add a to b, store in c
 Block statement:
{
// Swap a with b
temp = a;
a = b;
b = temp;
}
Prof. amr Goneid, AUC
5
3. Selection with if Statement
C = condition (logical: false/true, zero/non-zero)
e.g. a > b
S = simple or compound statement
 Syntax (1):
if (c) < s >;
e.g.
if (b > a) x = abs(a-b);
Prof. amr Goneid, AUC
C
T
F
S
6
if Statement
 Syntax (2):
if (c) s1; else s2;
e.g.
if (n == 0) sum = 0;
else
{
sum += x;
y = sum / n;
cout << n << y;
}
Prof. amr Goneid, AUC
C
T
F
S2
S1
7
Nested if Statements
 Example:
if (m >= 90) g = ‘A’;
else if (m >= 80) g = ‘B’;
else if (m >= 70) g = ‘C’;
else if (m >= 60) g = ‘D’;
else g = ‘F’;
Prof. amr Goneid, AUC
8
Conditions & Short Circuit
Evaluation
 A condition is built up using logical and
relational operators. It evaluates to true/false ,
(non-zero / zero) e.g.
 (a(a< <b)b)|| ||(c(c> >d)d)
if a is less than b then the whole condition is
true and (c > d) will not be evaluated.
 (c(c
>=>=
65)
&&
(c(c
<=<=
90)
65)
&&
90)
if c is less than 65 then the whole condition is
false and (c <= 90) will not be evaluated.
Prof. amr Goneid, AUC
9
4. The Ternary Operator
 The ternary operator will select to evaluate one of two
expressions e1 , e2 based on a condition c
 Syntax:
c ? e1 : e2 e.g. (k == 2) ? (x + 2) : (x + 5)
if k equals 2 the expression is evaluated as x + 2
otherwise it will be evaluated as x + 5.
 Example:
int x = 2; int k = 2;
y = ((k == 2) ? (x + 2) : (x + 5)) + 3; cout << y;
k++;
y = ((k == 2) ? (x + 2) : (x + 5)) + 3; cout << y;
Prof. amr Goneid, AUC
10
5. The switch Construct
Prof. amr Goneid, AUC
11
The switch Construct
 Syntax: e = ordinal Expression(int , char, bool)
switch (e)
{
case value1 : s1; break;
case value2 : s2; break;
…
default :
s;
//Optional
}
Prof. amr Goneid, AUC
12
switch Construct Example
char choice ;
cin >> choice;
switch (choice)
{
case ‘R’ : cout << “Red” ; break;
case ‘G’ : cout << “Green” ; break;
case ‘B’ : cout << “Blue” ; break;
default : cout << “Error”;
}
Prof. amr Goneid, AUC
13
Ommiting break
switch (choice)
{
case ‘R’ : cout << “Red” ;
case ‘G’ : cout << “Green” ;
case ‘B’ : cout << “Blue” ;
default : cout << “Error”;
}
Input : R
Input : G
Input : B
Input : Y
output: RedGreenBlueError
output: GreenBlueError
output: BlueError
output: Error
Prof. amr Goneid, AUC
14
More than one Label
switch (choice)
{
case ‘R’ :
case ‘r’ : cout << “Red” ; break;
case ‘G’ :
case ‘g’ : cout << “Green” ; break;
case ‘B’ :
case ‘b’ : cout << “Blue” ; break;
default : cout << “Error”;
}
Prof. amr Goneid, AUC
15
Example:
void main()
{
int choice;
cout << ”Assignment 1:\n”;
cout << ”Choose 1 to see the due date\n”;
cout << ”Choose 2 to see the maximum ”
<< ”mark\n”;
cout << ”Choose 3 to exit\n”;
do
{
cout << ”Enter a choice and press ”
<< ”return\n”;
cin >> choice;
Prof. amr Goneid, AUC
16
switch (choice)
{
case 1:
cout << ”The due date is 13/4/01\n”;
break;
case 2:
cout << ”The maximum mark is 20\n”;
break;
case 3:
cout << ”End of program\n”;
break;
default:
cout << ”Not a valid choice.\n”
<< ”Choose again, please.\n”;
}
} while (choice != 3);
} // End of program
Prof. amr Goneid, AUC
17
Sample Dialogue:
Assignment 1:
Choose 1 to see the due date
Choose 2 to see the maximum mark
Choose 3 to exit
Enter a choice and press return
1
The due date is 23/4/01
Enter a choice and press return
2
The maximum mark is 20
Enter a choice and press return
4
Not a valid choice. Choose again, please.
3
End of program
Prof. amr Goneid, AUC
18