You are on page 1of 43

BITG 1113:

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?

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 .

3
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.

4
Understand TRUE and FALSE

false

true true

0
True and false on the arithmetic scale

5
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

6
Logical Operators

7
Logical Operators

8
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

9
Operator Precedence

10
Type of Selection

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

11
Structure of if statements
The structure of an if statement is as follows:

if(condition)
true
{
Statement false
}

Notes : { } must be used for the compound statement

(compound statement contains more than one statement) 12


Example of if statements

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

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
}

14
Flowchart of if…else statements

Two way decision logic

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

Print a, “is an odd number”

End
17
Example of if…else statements

Example of if..else statement


18
Example of if…else statements

same as

A null else statement

19
Example of if…else statements

Compound statements in an if..else

20
Example of if…else statements
(with errors!)
if (score<40) if (score<40)

cout<<“Failed ”; cout<<“Failed ”;

cout<<“Work Harder”; cout<<“Work Harder”;

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;

Example of nested if code


26
Nested if statement

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

Dangling else solution 29


Example of dangling nested if solution

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";

(score>40) ? cout << "\nPassed” : cout << "\nFailed";


same as
cout <<(score>40) ? "\nPassed” : "\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

Read student’s test marks, marks

true Print “Grade


marks >=90 && marks <= 100
A”

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

Print “Not a valid input”

End 37
Multiple way selection
must be either bool value,
integer types , or a character

38
switch statement
Multiple way selection

Switch decision logic 39


40
Switch flow
A switch with break statements
41
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 42
}
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;
}

43

You might also like