computer science engineering 150a
play

Computer Science & Engineering 150A Introduction Problem - PowerPoint PPT Presentation

CSCE150A Computer Science & Engineering 150A Introduction Problem Solving Using Computers Relational Operators Lecture 04 - Conditionals Logical Operators Comparing Characters Stephen Scott if Statement Nested if (Adapted from


  1. CSCE150A Computer Science & Engineering 150A Introduction Problem Solving Using Computers Relational Operators Lecture 04 - Conditionals Logical Operators Comparing Characters Stephen Scott if Statement Nested if (Adapted from Christopher M. Bourke) Statements switch Statement Review Fall 2009 1 / 56

  2. CSCE150A Introduction Relational Operators Control Structure Logical Operators Conditions Comparing Characters if statements if Statement Nested if Statements switch Statement Review 2 / 56

  3. Control Structure CSCE150A Introduction Control structures : Relational Control the flow of execution in a program or function. Operators Enable you to combine individual instructions into a single logical unit Logical Operators with one entry point (i.e. int main(void) { ) and one exit point ( return 0; } ). Comparing Characters Three kinds of structures to control execution flow: if Statement Sequence Nested if Statements Selection Repetition switch Statement Review 3 / 56

  4. Sequential Flow Compound statement : CSCE150A Written as a group of statements Bracketed by { and } Introduction Relational Used to specify sequential flow Statement01; Operators All statements are unconditionally Logical Operators executed Comparing Characters Order is important Statement02; if Statement Nested if Statements switch Statement03; Statement Review . . . 4 / 56

  5. Selection Flow CSCE150A Selection control structure : Evaluates criteria to determine Introduction which alternative “path” to Relational Operators follow. Selection Logical Control A control structure determines Operators Structure which statement(s) to execute Comparing Characters Statements are mutually if Statement Statement01; Statement02; exclusive Nested if Statements switch Statement . . . Review 5 / 56

  6. Selection Flow – Conditions CSCE150A Definition A condition is an expression that is either true or false . Introduction Relational A program chooses alternative paths of computation by testing one or Operators more conditions. Logical Operators Comparing (ConditionEval == 1 ) → true , Characters if Statement (ConditionEval == 0 ) → false . Nested if The resting heart rate is a good indicator of health Statements if (resting heart rate < 75) then you are in good health. switch Statement if resting heart rate is 80, ConditionEval is false . Review if resting heart rate is 50, ConditionEval is true . if resting heart rate is 75, what is ConditionEval? 6 / 56

  7. Relational and Equality Operators CSCE150A Operator Meaning Type Introduction less than relational Relational < Operators greater than relational > Logical Operators less than or equal to relational <= Comparing greater than or equal to relational >= Characters == equal to equality if Statement not equal to equality != Nested if Statements Table: Relational and Equality Operators in C switch Statement Review 7 / 56

  8. Relational and Equality Operators CSCE150A Conditions come in four forms: variable relational-operator variable Introduction Relational Example: if(numberOfStudents > numberOfSeats) Operators Logical variable relational-operator CONSTANT Operators Example: if(numberOfStudents < 5) Comparing Characters variable equality-operator variable if Statement Example: if(numberOfStudents == numberOfSeats) Nested if Statements variable equality-operator CONSTANT switch Statement Example: if(averageGrade == 75.0) Review What about more than one condition? (Example: 0 ≤ x ≤ 10 ) 8 / 56

  9. Logical Operators CSCE150A Logical Operators : Operators that can combine conditions to make Introduction more complicated selection statements. Relational Operators Logical C Syntax Meaning True When Operators logical And Both are true && Comparing Characters || logical Or Either is true if Statement logical Not ( negation ) False ! Nested if Statements Table: Logical Operators in C switch Statement Review 9 / 56

  10. Logical Operators CSCE150A Logical Expressions - expressions that involve conditional statement(s) Introduction and logical operator(s). Relational Operators Examples: Logical Operators (x >= 0 && x <=10) Comparing Characters (temperature > 90.0 && humidity > 0.90) if Statement !(x >= 0 && x <=10) Nested if Statements What about the following: Are we going to go or not? switch Statement (go || !go) Review 10 / 56

  11. Tautologies & Contradictions CSCE150A Introduction A tautology is a logical expression that is always true Relational Any non-zero constant ( 1, 1.5, 8 , etc.) Operators An expression that, when simplified, always ends up being true Logical Operators (go || !go) is always true Comparing A contradiction is a logical expression that is always false Characters if Statement The zero constant ( 0 ) An expression that, when simplified, always ends up being false Nested if Statements (go && !go) is always false switch Statement Review 11 / 56

  12. Distributivity CSCE150A The logical And can be distributed over a logical expression just as multiplication can be over an algebraic expression. Introduction a ( b + c ) = ab + ac Relational a && (b || c) is same as (a && b) || (a && c) Operators (Here, a , b , and c are relations like x < 5 ) Logical Operators When distributing the logical Not , And and Or are reversed! Comparing Characters Example: if Statement !(x >= 0 && x <=10) Nested if (!(x >= 0) || !(x <=10)) Statements ((x < 0) || (x > 10)) switch Statement Review Best to simplify logical expressions as much as possible, but more important to keep code readable. 12 / 56

  13. True and False C Convention CSCE150A Introduction For convenience when writing we identify zero with false and one Relational with true Operators Logical C does not recognize the words true, false Operators C has no built-in Boolean type! Comparing Characters Instead, zero is identified with false if Statement Any non-zero value is identified with true Nested if Statements Example: -1, 0.01, 386 are all true switch Statement Review 13 / 56

  14. Operator Tables Logical And CSCE150A The result of taking a logical And with two operands is true if and only if Introduction both operands are true . Otherwise it is false . Relational Operators Logical Operators Operand A Operand B Result Comparing 0 0 0 Characters 0 1 0 if Statement 1 0 0 Nested if Statements 1 1 1 switch Statement Review 14 / 56

  15. Operator Tables Logical And CSCE150A The result of taking a logical Or with two operands is true if and only if Introduction at least one of the operands is true . Otherwise it is false . Relational Operators Logical Operators Operand A Operand B Result Comparing 0 0 0 Characters 0 1 1 if Statement 1 0 1 Nested if Statements 1 1 1 switch Statement Review 15 / 56

  16. Operator Tables Logical And CSCE150A Introduction You can only apply a logical Not to a single operand. The result is that Relational true gets flipped to false and vice versa. Operators Logical Operators Operand Result Comparing Characters 0 1 if Statement 1 0 Nested if Statements switch Statement Review 16 / 56

  17. Operator Precedence Order of precedence for operators CSCE150A Precedence Operator Introduction High Function calls Relational Operators (unary) ! + - & Logical * / % Operators + - (binary) Comparing Characters < <= >= > if Statement == != Nested if Statements && switch || Statement Low = Review Table: Order of Precedence for Operators 17 / 56

  18. Short-Circuiting CSCE150A If the first operand of a logical Or is true, the whole expression is Introduction true regardless of the second operand. Relational Operators Similarly, if the first operand of a logical And is false, the whole Logical expression is false regardless of the second operand. Operators Comparing (true || anything) is true Characters (false && anything) is false if Statement By convention, in either case C does not bother to evaluate the Nested if Statements second operand. switch This is known as short-circuiting Statement Review 18 / 56

  19. Programming Tip CSCE150A Introduction Writing pseudocode will help you to write logical expressions in plain Relational Operators English. Logical Operators Translate the expressions into valid C syntax Comparing Be sure that the original and the translation are logically equivalent Characters if Statement You can use a int type to store true/false: Nested if int someBoolean = 0; Statements switch Statement Review 19 / 56

  20. Comparing Characters CSCE150A Introduction Recall that C uses partially weak typing Relational Operators C treats characters as integers in the range [0 , 255] Logical Thus, it makes sense that we can compare characters using relational Operators and equality operators. Comparing Characters Comparisons are based on the values used to encode letters (typically if Statement ASCII; Appendix A) Nested if Statements Example: ’a’ < ’e’ is true since (in ASCII) 97 < 101 switch Statement Review 20 / 56

Recommend


More recommend