chapter 4 control structures i selection
play

Chapter 4: Control Structures I (Selection) C++ Programming 1 Dr. - PowerPoint PPT Presentation

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming Programming Fundamentals I Chapter 4: Control Structures I (Selection) C++ Programming 1 Dr. Adriana Badulescu Kallas Introduction to Computer Science &


  1. Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming Programming Fundamentals I Chapter 4: Control Structures I (Selection) C++ Programming 1

  2. Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming Programming Fundamentals I Objectives Explore how to form Examine relational Learn about control and evaluate logical and logical structures (Boolean) operators expressions Discover how to use Learn how to avoid Learn to use the the selection bugs by avoiding assert function to control structures if, partially understood terminate a if...else, and switch concepts program in a program C++ Programming 2

  3. Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming Programming Fundamentals I Control Structures � A computer can proceed: � In sequence � Selectively (branch): making a choice � Repetitively (iteratively): looping � Some statements are executed only if certain conditions are met � A condition is met if it evaluates to true C++ Programming 3

  4. Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming Programming Fundamentals I Control Structures true false Statement1 false true Expression Expression Statement2 Statement2 Statement1 Statement Statement3 Sequence Selection Repetition C++ Programming 4

  5. Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming Programming Fundamentals I Relational Operators � A condition is represented by a logical (Boolean) expression that can be true or false � Relational operators : � Allow comparisons between two operands (binary) � Evaluate to true or false C++ Programming 5

  6. Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming Programming Fundamentals I Relational Operators and Simple Data Types � You can use the relational operators with all three simple data types: evaluates to true � (8 < 15) evaluates to false � (6 != 6) evaluates to false � (2.5 > 5.8) evaluates to true � (5.9 <= 7.5) evaluates to true � (‘a’ == 97) evaluates to true � (97.0 == 97) � (‘A’ > ‘a’) evaluates to false(65<97) � (a = ‘0’) evaluates to true C++ Programming 6

  7. Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming Programming Fundamentals I Relational Operators and Simple Data Types � Logical (Boolean) expressions � Expression with relational operators � Returns an integer value of 1 if the logical expression evaluates to true � Returns an integer value of 0 otherwise cout << (‘a’ == 97); cout << (‘a’ > ‘z’); 101 cout << (97.0 == 97); C++ Programming 7

  8. Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming Programming Fundamentals I Relational Operators and the string Type • Relational operators can be applied to strings • Strings are compared character by character, starting with the first character • Comparison continues until either a mismatch is found or all characters are found equal • If two strings of different lengths are compared and the comparison is equal to the last character of the shorter string – The shorter string is less than the larger string C++ Programming 8

  9. Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming Programming Fundamentals I Relational Operators and the string Type “Hello” < “Hi” true “Hello” > “Hen” false “Air” < “An” true “Hello” == “hello” false “Air” <= “Bill” true “Hi” > “Bill” true “Hello” > “hello” false “Bill” >= “Billy” false “Big” <= “Bigger” true C++ Programming 9

  10. Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming Programming Fundamentals I Logical Operators and Logical Expressions � Logical (Boolean) operators enable you to combine logical expressions C++ Programming 10

  11. Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming Programming Fundamentals I Logical Operators and Logical Expressions � The negation (!) operator C++ Programming 11

  12. Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming Programming Fundamentals I Logical Operators and Logical Expressions � The AND (&&) Operator C++ Programming 12

  13. Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming Programming Fundamentals I Logical Operators and Logical Expressions � The OR ( || ) Operator C++ Programming 13

  14. Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming Programming Fundamentals I Logical Operators and Logical Expressions � Logical Operators AND (&&) and OR (||) have 2 symbols � If you use only one symbol, you will get the Bitwise operator: Bitwise AND (&) and Bitwise OR (|) which are done per bit and lead to different results 0 0 0 0 0 0 0 1 1= � 1 && 2 -> 1 0 0 0 0 0 0 1 0 2= � 1 & 2 -> 0 0 0 0 0 0 0 0 0 =0 C++ Programming 14

  15. Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming Programming Fundamentals I Order of Precedence � Relational and logical operators are evaluated from left to right � The associativity is left to right � Parentheses can override precedence C++ Programming 15

  16. Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming Programming Fundamentals I Order of Precedence 1. + (positive) , - (negative), ! (not) 2. ++ (increment), -- (decrement) 3. * (multiplication), / (division), % (modulus) 4. + (addition), - (subtraction) 5. <, <=, >, >= (relational comparison) 6. == (equal-to), != (not-equal-to) 7. && (and) 8. || (or) 9. = (assignment), +=, -=, *=, /=, %= (compound assignment) C++ Programming 16

  17. Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming Programming Fundamentals I Order of Precedence x = 9 && 8 == 7 < !6 * - 5 / 4 + 3 % 2 - 1 x = 9 && 8 == 7 < 0 * (- 5) / 4 + 3 % 2 - 1 x = 9 && 8 == 7 < 0 / 4 + 1 - 1 x = 9 && 8 == 7 < 0 + 1 - 1 x = 9 && 8 == 7 < 0 x = 9 && 8 == 0 x = 9 && 0 x = 0 C++ Programming 17

  18. Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming Programming Fundamentals I int and bool Data Type and Logical Expressions � Logical expressions evaluate to either true (= 1 ) or false (= 0 ) � The data type bool has logical (Boolean) values true and false bool legalAge = (age >= 21); � Earlier versions of C++ did not provide built-in data types that had Boolean values, so the int data type was used to manipulate logical (Boolean) expressions int legalAge = (age >= 21); C++ Programming 18

  19. Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming Programming Fundamentals I Selection: if and if...else � One-Way Selection � Two-Way Selection � Compound (Block of) Statements � Multiple Selections: Nested if � Comparing if...else Statements with a Series of if Statements C++ Programming 19

  20. Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming Programming Fundamentals I One-Way Selection � The syntax is: if (Expression) Statement � The Statement is executed if the value of the Expression is true � The Statement is bypassed if the value is false and the program goes to the next statement true false Expression Statement C++ Programming 20

  21. Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming Programming Fundamentals I One-Way Selection � Examples : grade = ‘F’; CORRECT – but we do need the grade = ‘F’; if (score >= 60) assignment before the if to make sure the grade will grade = ‘P’; have a value if the score is lower than 60 SYNTACTICALLY CORRECT but LOGICALLY if (score >= 60) INCORRECT – it will make the grade = ‘P’ for grade = ‘P’; scores larger than 60 but will not assign any value for scores smaller than 60 if score >= 60 INCORRECT – missing parentheses grade = ‘P’; SYNTACTICALLY CORRECT – it has a valid if with an if (score >= 60); empty statement – that does not do ( if (score >= grade = ‘P’; 60); ) , followed by an assignment grade = ‘P’ but LOGICALLY INCORRECT – it will make the grade = ‘P’ for any score so you need to delete the ; at the end of the first line C++ Programming 21

  22. Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming Programming Fundamentals I One-Way Selection C++ Programming 22

  23. Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming Programming Fundamentals I Two-Way Selection � If expression is true , if (Expression) Statement1 is executed; Statement1 otherwise, Statement2 is else executed Statement2 true false Expression Statement1 Statement2 C++ Programming 23

  24. Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming Programming Fundamentals I Two-Way Selection � Example: if (hours > 40.0) wages = 40.0 * rate + 1.5 * rate * (hours – 40.0); else wages = hours * rate; C++ Programming 24

Recommend


More recommend