Upload
paancute8982
View
58
Download
1
Embed Size (px)
Citation preview
1
BITG 1113:Control Technique
(Selection & Switch)
LECTURE 4
2
Objectives :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
3
Why Selection?
Allows you to control if a program enters a section of code or not based on whether a given condition is true or false.
Allows the program to select an action based upon the user's input.
For example, by using an if statement to check a user entered password, your program can decide whether a user is allowed access to the program .
4
Understand TRUE and FALSE
• The meaning of TRUE and FALSE in computer terminology.
• A true statement is one that evaluates to a nonzero number.
• A false statement evaluates to zero.
5
0
true
false
true
True and false on the arithmetic scale
Understand TRUE and FALSE
6
Understand TRUE and FALSE
• When you perform comparison checks, the operator will return a nonzero number if the comparison is true, or zero if the comparison is false.
• For example, the check 0==2 evaluates to 0. The
check 2==2 evaluates to a nonzero number
7
Logical Operators
8
Logical Operators
9
Relational Operators
• There are a number of operators that allow these checks.examples:
> greater than 5>4 is TRUE
< less than 4<5 is TRUE
>= greater than or equal 4>=4 is TRUE
<= less than or equal 3<=4 is TRUE
10
Operator Precedence
11
Three type of selection control statements :
Single way decision : if
Two way decision code : if ….else
Nested if statements
Multiple way : if ….else if….,
switch…case
Type of Selection
12
Structure of if statements
The structure of an if statement is as follows:
if(condition)
{
Statement
}
true
false
Notes : { } must be used for the compound statement
(compound statement contains more than one statement)
13
Example of if statements
Example :
void main()
{
int a,b=5;
cin>>a;
if (a < 10)
a = a * b;
cout<<a;
}
true
false
a<10a=a*b
Read a
Print a
Start
End
14
Structure of if…else statements
The structure of an if statement is as follows:
if(condition) {
statement//do all of this if condition is TRUE
} else {
statement //executed if the condition is FALSE }
15
Two way decision logic
Flowchart of if…else statements
16
• To determine odd or even number
Example of if…else statements
void main()
{
int a;
cin>>a;
if (a%2==0)
cout<<a<<“ is an even number”;
else
cout<<a<<“ is an odd number”;
}
17
• Flowchart
Example of if…else statements
true
false
a%2==0
Read a
Print a, “is an odd number”
Start
End
Print a, “is an even number”
18
Example of if..else statement
Example of if…else statements
19
A null else statement
same as
Example of if…else statements
20
Compound statements in an if..else
Example of if…else statements
21
if (score<40)
cout<<“Failed ”;
cout<<“Work Harder”;
else
cout<<“Passed”;
cout<<“Congratulations”;
Example of if…else statements(with errors!)
Notes : error! else without matching if
if (score<40)
cout<<“Failed ”;
cout<<“Work Harder”;
else
cout<<“Passed”;
cout<<“Congratulations”;
22Complemented if..then statements
23
A null if statement
same as same as
Example of if…else statements
24
Example of if…else statements
discount = 0;
if (price<100)
;
else
discount = price *0.3 ;
discount = 0;
if (!(price<100))
discount = price *0.3 ;
else
;
discount = 0;
if (!(price<100))
discount = price *0.3 ;
same as
same as
25Nested if statements
Nested if statement
truefalse
truefalse
26
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;
Example of nested if code
27Dangling else
Nested if statement
truefalse
truefalse
28
if (score>40)
if (mark==100)
cout << "\nFull Marks. Congratulations." << endl;
else
cout << "\nFailed. Must work harder" << endl;
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
29Dangling else solution
Nested if statement
30
Example of dangling nested if solution
if (score>40)
{
if (mark==100)
cout << "\nFull Marks. Congratulations." << endl;
}
else
cout << "\nFailed.Must work harder" << endl;
31Conditional expression
simplified if..else statement
Conditional Operator
32
if (score>40)
cout << "\nPassed”;
else
cout << "\nFailed";
Conditional Operator
(score>40) ? cout << "\nPassed” : cout << "\nFailed";
cout <<(score>40) ? "\nPassed” : "\nFailed";
discount = (answer ==‘y’ ? 0 : 15);
Example 1:
Example 2:
same as
33
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
Multiple way selection
34
f
tf
t
t
f
condition2
condition1
condition3
Multiple way selection
35
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“}
Multiple way selection
36
marks >=90 && marks <= 100
marks >=80 && marks < 90
Start
Read student’s test marks, marks
Example of multiple way selection
1 2
Print ”GradeB ”
Print “GradeA”
true
false
true
false
37
marks >= 70 && marks <80
1
marks >= 60 && marks < 70
marks >= 0 && marks <60
2
Print “Not a valid input”
End
Print ” GradeF ”
Print ” GradeD ”
Print ” GradeC ”
true
false
true
false
true
false
38
switch statement
Multiple way selection
must be either bool value, integer types , or a character
39Switch decision logic
Multiple way selection
40Switch flow
41A switch with break statements
42
switch (mark/10)
{
case 10 :
case 9 : grade = ‘A’;
break;
case 8 : grade = ‘B’;
break;
case 7 : grade = ‘C’;
break;
case 6 : grade = ‘D’;
break;
case 5 : case 4 : case 3 : case 2 : case 1 : case 0 :
grade = ‘F’; break;
default :
}Example of switch code
43
switch (selection)
{
case 'A' : cout << "\n To append a record" << endl;
break;
case 'M' : cout << "\n To modify a record" << endl;
break;
case 'D' : cout << "\n To delete a record" << endl;
break;
case 'X' : cout << "\n To exit the menu" << endl;
break;
default : cout << "\n Invalid selection" << endl;
}