1 / 9

Logic & Logical Operators

Logic & Logical Operators. Logical Operators are used to connect two or more relational expressions into one OR to reverse the logic of an expression. Operator Meaning && AND || OR ! NOT. && (AND).

alton
Download Presentation

Logic & Logical 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. Logic &Logical Operators Logical Operators are used to connect two or more relational expressions into one OR to reverse the logic of an expression. OperatorMeaning && AND || OR ! NOT

  2. && (AND) When && connects two relational expressions into one expression BOTH connecting relational expressions must evaluate to true for the final expression to evaluate to true. TWO relational expression operands are required. Examples: Given int w = 15, x = 0, y = 4, z = 3; Combination Individual Evaluation Result x != 0 && y > z false && true false w / z > y && y – x < 4 true && false false x + y < 0 && w <= 0 false && false false y / z == 1 && w / z > 0 true && true true Note: The computer evaluates the left-most relational expression first. If it evaluates to false it does not evaluate the right-most relational expression.

  3. || (OR) When || connects two relational expressions into one expression if EITHER connecting relational expression evaluates to true then the final expression also evaluates to true. TWO relational expression operands are required. Examples: Given int w = 15, x = 0, y = 4, z = 3; Combination Individual Evaluation Result x != 0 || y >z false || true true w / z > y || y – x < 4 true || false true x + y < 0 || w <= 0 false || false false y / z == 1 || w / z > 0 true || true true Note: The computer evaluates the left-most relational expression first. If it evaluates to true it does not evaluate the right-most relational expression.

  4. ! (NOT) ! (NOT) reverses the ‘truth’ of a relational expression. Only ONE relational expression operand is required. If the operand is false then ! combined with it evaluates to true. If the operand evaluates to true then ! combined with it evaluates to false. Examples: Given int w = 15, x = 0, y = 4, z = 3; Combination Operand Evaluation Result ! (x <= y) true false ! (z - y >= 0) false true Note: It is a good idea to always enclose the operand in () when using the ! logical operator.

  5. Precedence • ! has a higher precedence than most all other operators. Style: Enclose ! operands in () Examples: x = -5 ! (x > 0)  ! (false)  true ! x > 0  0 > 5  false LOOK OUT!!! • && and || have lower precedence than the relational operators • && has a higher precedence than || Note: Use of parenthesis is encouraged to make sure your expression is evaluated as you desire it to be.

  6. Comparing Char & String Review: How is type ‘char’ stored in memory? One byte (8 bits) is used for ‘char’ with ASCII codes (0 to 127) assigned for various characters. Comparison of ‘char’ values is a comparison of their ASCII code values.

  7. Codes for Numerals and Alphabetic Characters CharacterASCII value ‘0’ – ‘9’ 48 – 57 ‘A’ – ‘Z’ 65 – 90 ‘a’ – ‘z’ 97 – 122 Note: ‘6’ < ‘7’ because 54 < 55 ‘D’ < ‘E’ because 68 < 69 Also: ‘A’ < ‘a’ because 65 < 97

  8. String Storage & Comparison Strings are stored as consecutive char followed by the NULL (ASCII 0) character. The computer compares strings by comparing ASCII values of their component char Ex. Compare “Joey” and “Joseph” “Joey” < “Joseph” because 101 < 115

  9. Program Control Based on UNIQUE Valuesswitch Statement Syntax: switch (IntegerExpression) { case ConstantExpression1: stuff to do when IntegerExpression evaluates to ConstantExpression1 break; //Optional – see examples case ConstantExpression2: stuff to do when IntegerExpression evaluates to ConstantExpression2 break; . . . default: //Optional stuff to do when IntegerExpression evaluates to none of the ConstantExpression(n)s } Note: default is optional and so is break! Note2: If break is omitted then EVERY option after that is included (called ‘Fall Through’)

More Related