Loading in 5 sec....

Logic & Logical OperatorsPowerPoint Presentation

Logic & Logical Operators

- By
**alton** - Follow User

- 122 Views
- Uploaded on

Download Presentation
## PowerPoint Slideshow about ' Logic & Logical Operators' - alton

**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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript

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

&& (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.

|| (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.

! (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.

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.

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.

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

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

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’)

Download Presentation

Connecting to Server..