230 likes | 425 Views
Chapter 4 Control Structures I. (Selection). Objectives. Overview of control structure Relational and logical operators Logical expression if if … else switch. Control Structures. Flow Chart Components. Connector Start/end point Decision point Action Flow. true. false.
E N D
Chapter 4 Control Structures I (Selection)
Objectives • Overview of control structure • Relational and logical operators • Logical expression • ifif … elseswitch Chapter 4 Selection
Control Structures Chapter 4 Selection
Flow Chart Components • ConnectorStart/end point • Decision point • Action • Flow true false Chapter 4 Selection
Relational Operators • Evaluate to bool data type: true / false • bool is stored as int • true is represented as 1 • false is represented as 0 • All non-zero int is considered as true Don’t confuse ==with = Chapter 4 Selection
Evaluating Relational Expression • Integers & real numbers • Machine-dependent • char • ASCII collating sequence: predefined ordering of characters in the ASCII set (Appendix C) • string • Character-by-character • Lexicographically Chapter 4 Selection
Logical Operators & Expressions • Operators:! (not), && (and), || (or) • Evaluating Chapter 4 Selection
Order of Precedence Chapter 4 Selection
Complex Logical Expression • Always use ( ) to clarify the meaning!!! • How to express “0 < x < 10” • 0 < x < 10 • 0 < x && x < 10 • (0 < x) && (x < 10) Example 4-5 Chapter 4 Selection
bool found = true; bool flag = false; double x = 5.2; int a = 5, b = 8; !(found && (x >= 0)) !(found && true ) !(true && true ) !( true ) false (a + 2 <= b) && !flag ( 7 <= 8) && !flag ( true ) && !flag ( true ) && !false ( true ) && true true Chapter 4 Selection
One-Way Selection if (expression) statement; • If the value of the expression is true, statement is executed; • if the value is false, statement is not executed and the control goes on to the next statement in the program expression true statement false Chapter 4 Selection
Two-Way Selection if (expression) statement1; else statement2; • If the value of the expression is true, statement1 is executed; • If the value is false, statement2 is executed false expression true statement2 statement1 Chapter 4 Selection
Compound Statements • A sequence of statements enclosed in curly braces { } (aka block of statements) {statement1;statement2;…statementn; } Chapter 4 Selection
Multiple Selections: Nested if true score>=90? grade = “A” false true score>=80? grade = “B” false true score>=70? grade = “C” false true score>=60? grade = “D” false grade = “F” Chapter 4 Selection
Pairing else with if • Rule1: else is paired with the most recent if • Rule2: pairing cannot happen across curly braces (inside cannot be paired with outside) Example 4-20 Example 4-22 Chapter 4 Selection
true temp>=50? false false true temp>=80? play tennis golfing swimming Example 4-20 if(temperature >= 50) //Line 1if(temperature >= 80) //Line 2 cout<<"Good day for swimming."<<endl; //Line 3else//Line 4 cout<<"Good day for golfing."<<endl; //Line 5else//Line 6 cout<<"Good day to play tennis."<<endl; //Line 7 Chapter 4 Selection
Example 4-22 if(GPA >= 2.0) //Line 1if(GPA >= 3.9) //Line 2 cout<<"Dean\’s Honor List."<<endl; //Line 3else//Line 4cout <<"Below graduation requirement. "<<"\nSee your advisor."<<endl; //Line 5 if(GPA >= 2.0){//Line 1if(GPA >= 3.9) //Line 2 cout<<"Dean\’s Honor List."<<endl; //Line 3}else//Line 4cout <<"Below graduation requirement. "<<"\nSee your advisor."<<endl; //Line 5 Chapter 4 Selection
Advice • If the decision is made on a range of value, it makes life much easier to write the nested if structure in ascending or descending order of the value • Rewrite the code on the previous two slides Chapter 4 Selection
Input Failure and if • cin evaluates to • true if the last input succeeded • false if the last input failed • ifstream/ofstream variables evaluate to • true if the associated file was opened successfully • false if the associated file was not opened successfully Chapter 4 Selection
switch Structures • if…else involves evaluating logical expressionswitch does not involve evaluating logical expression • switch(expression){case value1: statements1;break; //optionalcase value2: statements2;break; //optional...case valuen: statementsn;break; //optionaldefault: statements; //optional} This is NOT evaluated to true/false but int value Chapter 4 Selection
The switch statement executes according to the following rules: • 1. When the value of the expression is matched against a case value (also called a label), the statements execute until either a break statement is found or the end of the switch structure is reached. • 2. If the value of the expression does not match any of the case values, the statements following the default label execute. If the switch structure has no default label, and if the value of the expression does not match any of the case values, the entire switch statement is skipped. • 3. A break statement causes an immediate exit from the switch structure. Chapter 4 Selection
switch Structures • Although it need not be, the expression is usually an identifier. • The value of the expression can be only integral. • The expression is sometimes called the selector. Its value determines which statement is selected for execution. • A particular case value should appear only once. • One or more statements may follow a case label, so you do not need to use braces to turn multiple statements into a single compound statement. • The break statement may or may not appear after each statement. Example 4-24, 25 Chapter 4 Selection