1 / 7

Relational Operators

Relational Operators. Operator Meaning < Less than > Greater than == Equal to <= Less than or equal to >= Greater than or equal to != Not equal to These are binary operators There must be expressions on either side of the operator

dtrisha
Download Presentation

Relational Operators

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. Relational Operators Operator Meaning < Less than > Greater than == Equal to <= Less than or equal to >= Greater than or equal to != Not equal to • These are binary operators • There must be expressions on either side of the operator • Those expressions should evaluate to comparable types. The comparisons will evaluate to true (1) or false (0) • Caution • The two-character relational operators must appear exactly as written above, with the two characters in the proper order and without intervening spaces. • Don’t confuse the assignment operator ( = ) with the relational operator ( == ).

  2. Relational ExpressionsNumeric Expression Meaning Value 4 <= 5 4 less than or equal to 5 true(1) 4 != 4 4 not equal to 4 false(0) 4.0 == 4 real 4.0 equals integer 4 true(1) 4.0 >= 4.0 4.0 greater than or true(1) equal to 4.0 4.0 > 5.0 4.0 greater than 5.0 false(0)

  3. Relational ExpressionsASCII Characters Expression Meaning Value ‘B’ > ‘A’ ‘B’ greater than ‘A’ true(1) ‘g’ < ‘h’ ‘g’ less than ‘h’ true (1) ‘1’ < ‘2’ ‘1’ less than ‘2’ true(1) ‘B’ == ‘b’ ‘B’ equal to ‘b’ false(0) ‘B’ != ‘b’ ‘B’ not equal to ‘b’ true(1) ‘B’ > ‘a’ ‘B’ greater than ‘a’ false(0) ‘B’ > ‘1’ ‘B’ greater than ‘1’ true(1) ‘?’ > ‘!’ ‘?’ greater than ‘!’ true(1)

  4. Logical Operators • The three logical operators allow for creating more complex comparisons. Operator Meaning Type of operator ! Not unary && and binary || or binary

  5. Logical Truth Tables • Semantics of boolean expressions can be described by truth tables (where p and q represent boolean expressions) p !p NOT truth tables true false false true p q p && qAND truth table true true true true false false false true false false false false p q p || q _OR truth table true true true true false true false true true false false false

  6. Understanding Boolean Logic • Suppose that char ans = ‘Y’; int sum = 23; int index = 17; double r = 7.5; int done = true; • Evaluate (ans == ‘Y’) && (index != 0) ______ !(ans < ‘z’) ______ (sum == 45) || (index < r) ______ ( !done) && (index < 30) ______ done && (sum > 21) ______ (r <= r) || (!done) ______ (sum <= 40) || done && !(done) ______

  7. Operator Precedence Operator Precedence Function calls highest ! + - & (Unary operators) * / % + - < <= >= > == != && || = lowest • Note: You may use parentheses to change the order of evaluation.

More Related