Professional Documents
Culture Documents
Control Technique
(Selection & Switch)
LECTURE 4
1
Objectives :
1. To understand the concept of
selection control structure
2. To review the logical and relational
operators
3. To understand if, if…else, nested if
and multi way selection
2
Why Selection?
3
Understand TRUE and FALSE
4
Understand TRUE and FALSE
false
true true
0
True and false on the arithmetic scale
5
Understand TRUE and FALSE
6
Logical Operators
7
Logical Operators
8
Relational Operators
9
Operator Precedence
10
Type of Selection
11
Structure of if statements
The structure of an if statement is as follows:
if(condition)
true
{
Statement false
}
Example :
void main() Start
{
Read a
int a,b=5;
true
cin>>a; a=a*b
a<10
if (a < 10) false
a = a * b;
Print a
cout<<a;
} End
13
Structure of if…else statements
if(condition)
{
statement
//do all of this if condition is TRUE
}
else
{
statement
//executed if the condition is FALSE
}
14
Flowchart of if…else statements
15
Example of if…else statements
• To determine odd or even number
void main()
{
int a;
cin>>a;
if (a%2==0)
cout<<a<<“ is an even number”;
else
cout<<a<<“ is an odd number”;
} 16
Example of if…else statements
• Flowchart
Start
Read a
true
a%2==0 Print a, “is an even
number”
false
End
17
Example of if…else statements
same as
19
Example of if…else statements
20
Example of if…else statements
(with errors!)
if (score<40) if (score<40)
cout<<“Failed ”; cout<<“Failed ”;
else
Notes : error! else
cout<<“Passed”; else without matching if
cout<<“Congratulations”; cout<<“Passed”;
cout<<“Congratulations”;
21
Complemented if..then statements
22
Example of if…else statements
same as same as
A null if statement
23
Example of if…else statements
discount = 0; discount = 0;
if (price<100) if (!(price<100))
;
same as discount = price *0.3 ;
else else
discount = price *0.3 ; ;
same as
discount = 0;
if (!(price<100))
discount = price *0.3 ;
24
Nested if statement
false true
false true
Nested if statements 25
if (job_title == 'a') // a = associate professor
if (year_served > 5)
if (no_of_publications > 7)
cout << "\nPromote to Professor" endl;
else
cout << "\nMore publications required" << endl;
else
cout << "\nMore service required" << endl;
else
cout << "\nMust become associate professor first" << endl;
false true
false true
Dangling else 27
Example of dangling nested if
if (score>40)
if (mark==100)
cout << "\nFull Marks. Congratulations." << endl;
else
cout << "\nFailed. Must work harder" << endl;
same as
if (score>40)
{ if (mark==100)
cout << "\nFull Marks. Congratulations." << endl;
else
cout << "\nFailed. Must work harder" << endl;
} 28
Nested if statement
if (score>40)
{
if (mark==100)
cout << "\nFull Marks. Congratulations." << endl;
}
else
cout << "\nFailed.Must work harder" << endl;
30
Conditional Operator
simplified if..else
statement
31
Conditional expression
Conditional Operator
Example 1:
if (score>40)
cout << "\nPassed”;
else
cout << "\nFailed";
Example 2:
discount = (answer ==‘y’ ? 0 : 15);
32
Multiple way selection
The structure of a multiple way statement (else if..) is as follows:
if(condition1)
{
statement //do all of this if condition1 is true
}
else if(condition2)
{
statement //executed if condition1 is false
}
else if(condition3)
{
statement
//executed if condition1 and condition2 are false
}
else
statement
//executed if all the conditions are false
33
Multiple way selection
t
condition1
f
t
condition2
f
condition3
t
34
Multiple way selection
void main( )
{
int marks;
char Grade;
cout<<“Please enter the test marks for the student :”
cin>>marks;
if (marks >=90 && marks <= 100)
cout<<“Gred A”;
else if (marks >=80 && marks < 90)
cout<<“Gred B”;
else if (marks >= 70 && marks <80)
cout<<“Gred C”;
else if (marks >= 60 && marks < 70)
cout<<“Gred D”;
else if (marks >= 0 && marks <60)
cout<<“Gred F”;
else
cout<< “ Not a valid input“
}
35
Example of multiple way selection
Start
false
true
marks >=80 && marks < 90 Print ”Grade
B”
false
1 2
36
1 2
true
marks >= 70 && marks <80 Print ” Grade
C”
false
true
marks >= 60 && marks < 70 Print ” Grade
D”
false
true
Print ” Grade
marks >= 0 && marks <60
F”
false
End 37
Multiple way selection
must be either bool value,
integer types , or a character
38
switch statement
Multiple way selection
43