43
1 BITG 1113: Control Technique (Selection & Switch) LECTURE 4

C++ Selection

Embed Size (px)

Citation preview

Page 1: C++ Selection

1

BITG 1113:Control Technique

(Selection & Switch)

LECTURE 4

Page 2: C++ Selection

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

Page 3: C++ 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 .

Page 4: C++ Selection

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.

Page 5: C++ Selection

5

0

true

false

true

True and false on the arithmetic scale

Understand TRUE and FALSE

Page 6: C++ Selection

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

Page 7: C++ Selection

7

Logical Operators

Page 8: C++ Selection

8

Logical Operators

Page 9: C++ Selection

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

Page 10: C++ Selection

10

Operator Precedence

Page 11: C++ Selection

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

Page 12: C++ 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)

Page 13: C++ Selection

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

Page 14: C++ Selection

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 }

Page 15: C++ Selection

15

Two way decision logic

Flowchart of if…else statements

Page 16: C++ Selection

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

}

Page 17: C++ Selection

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”

Page 18: C++ Selection

18

Example of if..else statement

Example of if…else statements

Page 19: C++ Selection

19

A null else statement

same as

Example of if…else statements

Page 20: C++ Selection

20

Compound statements in an if..else

Example of if…else statements

Page 21: C++ Selection

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

Page 22: C++ Selection

22Complemented if..then statements

Page 23: C++ Selection

23

A null if statement

same as same as

Example of if…else statements

Page 24: C++ Selection

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

Page 25: C++ Selection

25Nested if statements

Nested if statement

truefalse

truefalse

Page 26: C++ Selection

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

Page 27: C++ Selection

27Dangling else

Nested if statement

truefalse

truefalse

Page 28: C++ Selection

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

Page 29: C++ Selection

29Dangling else solution

Nested if statement

Page 30: C++ Selection

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;

Page 31: C++ Selection

31Conditional expression

simplified if..else statement

Conditional Operator

Page 32: C++ Selection

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

Page 33: C++ Selection

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

Page 34: C++ Selection

34

f

tf

t

t

f

condition2

condition1

condition3

Multiple way selection

Page 35: C++ 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

Page 36: C++ 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

Page 37: C++ Selection

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

Page 38: C++ Selection

38

switch statement

Multiple way selection

must be either bool value, integer types , or a character

Page 39: C++ Selection

39Switch decision logic

Multiple way selection

Page 40: C++ Selection

40Switch flow

Page 41: C++ Selection

41A switch with break statements

Page 42: C++ Selection

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

Page 43: C++ Selection

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;

}