1 / 23

Chapter 4 Control Structures I

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.

elisha
Download Presentation

Chapter 4 Control Structures I

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 4 Control Structures I (Selection)

  2. Objectives • Overview of control structure • Relational and logical operators • Logical expression • ifif … elseswitch Chapter 4 Selection

  3. Control Structures Chapter 4 Selection

  4. Flow Chart Components • ConnectorStart/end point • Decision point • Action • Flow true false Chapter 4 Selection

  5. 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

  6. 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

  7. Chapter 4 Selection

  8. Logical Operators & Expressions • Operators:! (not), && (and), || (or) • Evaluating Chapter 4 Selection

  9. Order of Precedence Chapter 4 Selection

  10. 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

  11. 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

  12. 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

  13. 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

  14. Compound Statements • A sequence of statements enclosed in curly braces { } (aka block of statements) {statement1;statement2;…statementn; } Chapter 4 Selection

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

  16. 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

  17. 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

  18. 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

  19. 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

  20. 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

  21. 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

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

  23. 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

More Related