Logical Operators - PowerPoint PPT Presentation

1 / 9
About This Presentation
Title:

Logical Operators

Description:

AND is represented by a double ampersand (&&) Evaluates two separate conditions. Evaluates to true only if both conditions evaluate to true ... – PowerPoint PPT presentation

Number of Views:58
Avg rating:3.0/5.0
Slides: 10
Provided by: swea9
Category:

less

Transcript and Presenter's Notes

Title: Logical Operators


1
Logical Operators
  • Computer Science
  • Chapter 5

2
Logical Operators
  • Logical Operators are used in Java similar to the
    way they are used in English.

3
Logical Operator AND
  • AND is represented by a double ampersand ()
  • Evaluates two separate conditions
  • Evaluates to true only if both conditions
    evaluate to true
  • if (( x gty) (xlt100)) // then take action

4
Logical OR Operator
  • Or operator is represented by
  • Evaluates two conditions
  • Will evaluate to true is either condition is true
  • if ( (xgty) (xlt100) if x is greater than y or
    x is less than 100, take action

5
Logical NOT
  • Logical NOT is represented by !
  • Evaluates to true if the expression is false
  • if ( !(xgty)) // if x is not greater than y take
    action

6
Truth Tables
  • Truth tables are often used to map the results of
    logical operations.

7
Short Circuit Evaluation
  • The Java Virtual Machine evaluates from left to
    right.
  • AND Operator
  • First expression evaluates to false
  • Second statement will not be evaluated because
    both need to be true
  • This is called short circuit evaluation

8
Division By Zero
  • Trying to divide a value by zero will generate an
    error
  • Take advantage of short circuit evaluation
  • first testing the divisor to see if it is equal
    to zero
  • if ((yValue ! 0) ( ( xValue / yValue)lt100))
    // the complete action

9
Short Circuit Evaluation OR Statement
  • If the left side of an OR statement is true
  • The right side does not need to be evaluated
  • Only one side of the expression has to be true
    when using the OR operator
  • if (amountRequestedgt 300 amountRequestedgt
    getBalance())
  • displayErrorBox()
Write a Comment
User Comments (0)
About PowerShow.com