Lecture 5 Selection Control Structures Selection Control Structures Dr. Hebbat Allah A. Elwishy...
-
Upload
maurice-farmer -
Category
Documents
-
view
225 -
download
1
Transcript of Lecture 5 Selection Control Structures Selection Control Structures Dr. Hebbat Allah A. Elwishy...
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]
Lecture 5 TopicsLecture 5 Topics
If statement, more examplesIf statement, more examples Nested StructuresNested Structures Switch statementSwitch statement
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
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
!! &&&& ||||
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
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
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.
ExampleExample
if (taxCode == ‘T’)
price = price + taxRate * price;
if ( A > 0 && A < 5 )
B = 1 / A ;
else
B = A;
What output? and Why?What output? and Why?
int age;
age = 20;
if ( age == 16 ){
cout<<( “Did you get driver’s license?”) ;}
What output? and Why?What output? and Why?
int age;
age = 30;
if ( age < 18 )
cout<< ( “Do you drive?”);
cout<< ( “Too young to vote”);
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.
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.
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
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 . . .
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
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.
Answer (1)Answer (1)
if (number > 0)
cout << ( “Positive” );
else if (number < 0)
cout << ( “Negative” );
else
cout << ( “Zero” ) ;
Answer (2)Answer (2)
if ( index < 35 )
cout << ( “Pleasant” );
else if ( index <= 60 )
cout << ( “Unpleasant” );
else
cout << ( “Health Hazard” ) ;
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.
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’;
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;}
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.
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 << "???";
}
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.
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 “);}