Lecture 5 Selection Control Structures Selection Control Structures Dr. Hebbat Allah A. Elwishy...

25
Lecture 5 Lecture 5 Selection Control Selection Control Structures Structures Dr. Hebbat Allah A. Dr. Hebbat Allah A. Elwishy Elwishy Computer & IS Assistant Computer & IS Assistant Professor Professor Email:

Transcript of Lecture 5 Selection Control Structures Selection Control Structures Dr. Hebbat Allah A. Elwishy...

Page 1: Lecture 5 Selection Control Structures Selection Control Structures Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor Email: hbbtllh_elwishy@yahoo.com.

Lecture 5 Lecture 5 Selection Control StructuresSelection Control Structures

Dr. Hebbat Allah A. Dr. Hebbat Allah A. ElwishyElwishy

Computer & IS Assistant ProfessorComputer & IS Assistant Professor

Email: [email protected]

Page 2: Lecture 5 Selection Control Structures Selection Control Structures Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor Email: hbbtllh_elwishy@yahoo.com.

Lecture 5 TopicsLecture 5 Topics

If statement, more examplesIf statement, more examples Nested StructuresNested Structures Switch statementSwitch statement

Page 3: Lecture 5 Selection Control Structures Selection Control Structures Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor Email: hbbtllh_elwishy@yahoo.com.

Selection statementsSelection statements

are used to choose an actionare used to choose an action

depending on the current situationdepending on the current situation

in your program as it is runningin your program as it is running

Page 4: Lecture 5 Selection Control Structures Selection Control Structures Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor Email: hbbtllh_elwishy@yahoo.com.

Control structuresControl structures

use logical expressions which may include:use logical expressions which may include:

6 Relational Operators6 Relational Operators

< < <= > >= <= > >= == != == !=

3 Logical Operators3 Logical Operators

!! &&&& ||||

Page 5: Lecture 5 Selection Control Structures Selection Control Structures Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor Email: hbbtllh_elwishy@yahoo.com.

IfIf statement is a selection statement is a selection

of whether or not to execute a statement (which of whether or not to execute a statement (which can be a single statement or an entire block)can be a single statement or an entire block)

TRUE

FALSEstatement

expression

Page 6: Lecture 5 Selection Control Structures Selection Control Structures Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor Email: hbbtllh_elwishy@yahoo.com.

if ( if ( Expression Expression ))

StatementStatement

NOTE: Statement can be a single statement, NOTE: Statement can be a single statement, a null statement, or a block.a null statement, or a block.

IfIf Syntax Syntax

Page 7: Lecture 5 Selection Control Structures Selection Control Structures Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor Email: hbbtllh_elwishy@yahoo.com.

Write Write IfIf or or If-ElseIf-Else for each for each

If taxCode is ‘T’, increase price by adding If taxCode is ‘T’, increase price by adding taxRate times price to it.taxRate times price to it.

If age is bigger than 60, give a discount of If age is bigger than 60, give a discount of 10% on a purchase.10% on a purchase.

If A is If A is strictlystrictly between 0 and 5, set B equal to between 0 and 5, set B equal to 1/A, otherwise set B equal to A.1/A, otherwise set B equal to A.

Page 8: Lecture 5 Selection Control Structures Selection Control Structures Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor Email: hbbtllh_elwishy@yahoo.com.

ExampleExample

if (taxCode == ‘T’)

price = price + taxRate * price;

if ( A > 0 && A < 5 )

B = 1 / A ;

else

B = A;

Page 9: Lecture 5 Selection Control Structures Selection Control Structures Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor Email: hbbtllh_elwishy@yahoo.com.

What output? and Why?What output? and Why?

int age;

age = 20;

if ( age == 16 ){

cout<<( “Did you get driver’s license?”) ;}

Page 10: Lecture 5 Selection Control Structures Selection Control Structures Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor Email: hbbtllh_elwishy@yahoo.com.

What output? and Why?What output? and Why?

int age;

age = 30;

if ( age < 18 )

cout<< ( “Do you drive?”);

cout<< ( “Too young to vote”);

Page 11: Lecture 5 Selection Control Structures Selection Control Structures Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor Email: hbbtllh_elwishy@yahoo.com.

Both the if clause and the else clauseBoth the if clause and the else clause

of an if...else statement can contain of an if...else statement can contain any kind of statement, including any kind of statement, including another selection statement.another selection statement.

Page 12: Lecture 5 Selection Control Structures Selection Control Structures Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor Email: hbbtllh_elwishy@yahoo.com.

Multi-alternative SelectionMulti-alternative Selection

is also called is also called multi-way branchingmulti-way branching,, and and

can be accomplished by using NESTED if can be accomplished by using NESTED if statements.statements.

Page 13: Lecture 5 Selection Control Structures Selection Control Structures Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor Email: hbbtllh_elwishy@yahoo.com.

if ( Expression1 )

Statement1

else if ( Expression2 )

Statement2.

.

.

else if ( ExpressionN )

StatementN

else

Statement N+1

EXACTLY 1 of these statements will be executed.EXACTLY 1 of these statements will be executed.

Nested if StatementsNested if Statements

Page 14: Lecture 5 Selection Control Structures Selection Control Structures Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor Email: hbbtllh_elwishy@yahoo.com.

Nested if StatementsNested if Statements

Each Expression is evaluated in sequence, until Each Expression is evaluated in sequence, until some Expression is found that is true. some Expression is found that is true.

Only the specific Statement following that particular Only the specific Statement following that particular true Expression is executed.true Expression is executed.

If no Expression is true, the Statement following the If no Expression is true, the Statement following the final else is executed.final else is executed.

Actually, the final else and final Statement are Actually, the final else and final Statement are optional. If omitted, and no Expression is true, optional. If omitted, and no Expression is true, then no Statement is executed.then no Statement is executed.

AN EXAMPLE . . .

Page 15: Lecture 5 Selection Control Structures Selection Control Structures Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor Email: hbbtllh_elwishy@yahoo.com.

if ( creditsEarned >= 90 )

cout << (“Fourth year student “ ) ;

else if ( creditsEarned >= 60 )

cout << ( “Third year student “ ) ;

else if ( creditsEarned >= 30 )

cout << ( “Second year student “ ) ;

else

cout << ( “First year student “ ) ;

Multi-way BranchingMulti-way Branching

Page 16: Lecture 5 Selection Control Structures Selection Control Structures Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor Email: hbbtllh_elwishy@yahoo.com.

Writing Nested if StatementsWriting Nested if Statements

1.1. Display one word to describe the int value of Display one word to describe the int value of number as “Positive”, “Negative”, or “Zero”.number as “Positive”, “Negative”, or “Zero”.

2.2. Your city classifies a pollution index Your city classifies a pollution index

less than 35 as “Pleasant”, less than 35 as “Pleasant”,

35 through 60 as “Unpleasant”,35 through 60 as “Unpleasant”,

and above 60 as “Health Hazard.” and above 60 as “Health Hazard.”

Display the correct description of theDisplay the correct description of the

pollution index value.pollution index value.

Page 17: Lecture 5 Selection Control Structures Selection Control Structures Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor Email: hbbtllh_elwishy@yahoo.com.

Answer (1)Answer (1)

if (number > 0)

cout << ( “Positive” );

else if (number < 0)

cout << ( “Negative” );

else

cout << ( “Zero” ) ;

Page 18: Lecture 5 Selection Control Structures Selection Control Structures Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor Email: hbbtllh_elwishy@yahoo.com.

Answer (2)Answer (2)

if ( index < 35 )

cout << ( “Pleasant” );

else if ( index <= 60 )

cout << ( “Unpleasant” );

else

cout << ( “Health Hazard” ) ;

Page 19: Lecture 5 Selection Control Structures Selection Control Structures Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor Email: hbbtllh_elwishy@yahoo.com.

ExampleAn Air Conditioner is designed to turn on : between 7 a.m. and 6 p.m.

If the temperature Exceeds 75 ° F and the humidity exceeds 40 %,

or if the temperature exceeds 70 ° F and the humidity exceeds 80 %;or

 between 6 p.m. and 7 a.m.

If the temperature exceeds 80 ° F and the humidity exceeds 80 %,orIf the temperature exceeds 85 ° F regardless of the humidity.

Develop a program that input temperature, humidity, and time of day and displays a message specifying whether the air conditioner is on or off.

Page 20: Lecture 5 Selection Control Structures Selection Control Structures Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor Email: hbbtllh_elwishy@yahoo.com.

Answer

#include <iostream>int main () {float tim, tem, hm; // declare the variables for time,

temperature and humiditychar status = ‘ ‘; // status of air conditioner on =‘N’ or off

=‘F’if (tim >= 7 && tim << 18)

if ( tem > 75 && hm > 40)status = ‘N’

else if ( tem > 70 && hm > 80)status = ‘N’

else status = ‘F’;

Page 21: Lecture 5 Selection Control Structures Selection Control Structures Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor Email: hbbtllh_elwishy@yahoo.com.

Answer (Con.)

else // next time, we don’t need to check it

if ( tem > 80 && hm > 80)status = ‘N’

else if ( tem > 85)status = ‘N’

else status = ‘F’;

cout << “the air conditioner now is: “<< status;return 0;}

Page 22: Lecture 5 Selection Control Structures Selection Control Structures Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor Email: hbbtllh_elwishy@yahoo.com.

Switch statementSwitch statement

switch ( integer_expr ) {

case constant_expr1:

statement1;

break;

case constant_expr2:

statement2;

break;

[default:

statement3;]

}

– Useful to select from a number of alternative integer values

– Constant_exprs must be byte, int, char, or short.

Page 23: Lecture 5 Selection Control Structures Selection Control Structures Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor Email: hbbtllh_elwishy@yahoo.com.

More on the switch Statement

– Case labels must be constants.

– Use break to jump out of a switch.

– It is recommended to always provide a default.

switch (choice)

{

case 37:

cout << "Coffee?";

break;

case 45:

cout << "Tea?";

break;

default:

cout << "???";

}

Page 24: Lecture 5 Selection Control Structures Selection Control Structures Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor Email: hbbtllh_elwishy@yahoo.com.

break vs returnbreak vs return

breakbreak means exit the switch means exit the switch statement and continue on with statement and continue on with the rest of the program.the rest of the program.

returnreturn means exit the whole means exit the whole program.program.

They could both be used anywhere They could both be used anywhere in the program.in the program.

Page 25: Lecture 5 Selection Control Structures Selection Control Structures Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor Email: hbbtllh_elwishy@yahoo.com.

ExampleExamplechar grade ;cout << (“Enter your letter grade: “);cin >> grade;switch ( grade ){ case ‘A’ : cout << (“ Excellent Job”); break; case ‘B’ : cout << ( “ Very Good “); break; case ‘C’ : cout << (“ Not bad “); break; case ‘F’ : cout << (“Failing”); break; default : cout << (“ Wrong Input “);}