Logic logical operators
Download
1 / 9

Logic & Logical Operators - PowerPoint PPT Presentation


  • 122 Views
  • Uploaded on

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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
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
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
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
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
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
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 values s witch statement
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’)


ad