Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Lecture 5 Selection Control Structures Selection Control Structures Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor"— Presentation transcript:

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

2 Lecture 5 Topics u If statement, more examples u Nested Structures u Switch statement

3 Selection statements are used to choose an action depending on the current situation in your program as it is running

4 Control structures use logical expressions which may include: 6 Relational Operators >= == != >= == != 3 Logical Operators !&&||

5 If statement is a selection of whether or not to execute a statement (which can be a single statement or an entire block) TRUE FALSE statement expression

6 if ( Expression ) Statement NOTE: Statement can be a single statement, a null statement, or a block. If Syntax

7 Write If or If-Else for each If taxCode is ‘T’, increase price by adding taxRate times price to it. If age is bigger than 60, give a discount of 10% on a purchase. If age is bigger than 60, give a discount of 10% on a purchase. If A is strictly between 0 and 5, set B equal to 1/A, otherwise set B equal to A.

8 Example if (taxCode == ‘T’) price = price + taxRate * price; if ( A > 0 && A < 5 ) B = 1 / A ; else B = A;

9 What output? and Why? int age; age = 20; if ( age == 16 ) { cout<<( “Did you get driver’s license?”) ; }

10 What output? and Why? int age; age = 30; if ( age < 18 ) cout<< ( “Do you drive?”); cout<< ( “Too young to vote”);

11 Both the if clause and the else clause of an if...else statement can contain any kind of statement, including another selection statement.

12 Multi-alternative Selection is also called multi-way branching, and can be accomplished by using NESTED if statements.

13 if ( Expression1 ) Statement1 else if ( Expression2 ) Statement2. else if ( ExpressionN ) StatementN else Statement N+1 EXACTLY 1 of these statements will be executed. Nested if Statements

14 Each Expression is evaluated in sequence, until some Expression is found that is true. Only the specific Statement following that particular true Expression is executed. If no Expression is true, the Statement following the final else is executed. Actually, the final else and final Statement are optional. If omitted, and no Expression is true, then no Statement is executed. AN EXAMPLE...

15 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 Branching

16 Writing Nested if Statements 1. Display one word to describe the int value of number as “Positive”, “Negative”, or “Zero”. 2. Your city classifies a pollution index less than 35 as “Pleasant”, 35 through 60 as “Unpleasant”, and above 60 as “Health Hazard.” and above 60 as “Health Hazard.” Display the correct description of the pollution index value.

17 Answer (1) if (number > 0) cout << ( “Positive” ); else if (number < 0) cout << ( “Negative” ); else cout << ( “Zero” ) ;

18 Answer (2) if ( index < 35 ) cout << ( “Pleasant” ); else if ( index <= 60 ) cout << ( “Unpleasant” ); else cout << ( “Health Hazard” ) ;

19 Example An 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 %, or If 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.

20 Answer #include int main () { float tim, tem, hm; // declare the variables for time, temperature and humidity char 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’;

21 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; }

22 Switch 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.

23 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 << "???"; }

24 break vs return n break means exit the switch statement and continue on with the rest of the program. n return means exit the whole program. n They could both be used anywhere in the program.

25 Example char 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 “); }


Download ppt "Lecture 5 Selection Control Structures Selection Control Structures Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor"

Similar presentations


Ads by Google