Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— 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 EvaluationResult 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 EvaluationResult x != 0 || y >z false || truetrue w / z > y || y – x < 4 true || false true x + y < 0 || w <= 0 false || falsefalse y / z == 1 || w / z > 0 true || truetrue 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 EvaluationResult ! (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  falseLOOK 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 Character ASCII 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 JoeyNULL 741111011210 JosephNULL 741111151011121040

9 Program Control Based on UNIQUE Values switch 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’)


Download ppt "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."

Similar presentations


Ads by Google