Skip to main content
Lesson 8 - Structured Programming, Control Structures, if-else Statements, Pseudocode
ZIPPDF (letter)
Lesson MenuPreviousNext
  
Logical Operators page 7 of 15

  1. The three logical operators of programming are AND, OR, and NOT. These operators are represented by the following symbols in Java:

    AND		&&
    OR		|| (two vertical bars)
    NOT		!
  2. The && (and) operator requires both operands (values) to be true for the result to be true.

    T  and  T  =  true
    T  and  F  =  false
    F  and  T  =  false
    F  and  F  =  false
  3. The following are Java examples of using the && (and) operator.

    ( (2 < 3) && (3.5 > 3.0) )   evaluates as true
    ( (1 == 0) && (2 != 3) )     evaluates as false

    The && operator performs short-circuit evaluation in Java. If the first half of an && statement is false, the operator immediately returns false without evaluating the second half.

  4. The || (or) operator requires only one operand (value) to be true for the result to be true.

    T  or  T  =  true
    T  or  F  =  true
    F  or  T  =  true
    F  or  F  =  false
  5. The following is a Java example of using the || (or) operator.

    ( (2+3 < 10) || (21 > 19) )      evaluates as true

    The || operator also performs short-circuit evaluation in Java. If the first half of an || statement is true, the operator immediately returns true without evaluating the second half.

  6. The ! operator is a unary operator that changes a boolean value to its opposite.

    !false  =  true
    !true  =  false

Lesson MenuPreviousNext
Contact
 ©ICT 2003, All Rights Reserved.