Logical expressions

 

Before we consider other variations of the if statement, we’re going to look at how to construct more complex logical conditions.  So far, the logical conditions we’ve seen –     whether as a loop or an if statement – relate or compare two quantities.  We’ve been able to express questions such as “is a greater than b?” or “does x equal y?”.  Unfortunately, this logic does not allow us to express situations that involve multiple comparisons.  For instance, we may want to be able to express something like “is x greater than 5 AND y less than 10?”.  Another example would be “is either x less than 10 OR greater than 20?”.  Such logical conditions require the definition of new operators, which combine multiple relational statements (those involving <, >, <=, etc.) to form a logical expression.  The new operators are called Boolean operators and are as follows:

 

                        Symbol                                    What it stands for

                        &&                                            Logical ‘and’

                        || (two vertical bars)            Logical ‘or’

                        !                       Logical negation

           

A logical expression, just as a relational statement, evaluates to either true or false (a Boolean value).  Its truth value then depends on the truth value of the relational statements.  For each of the Boolean operators, we list all the possible truth values of two relational statements and the truth value of the logical expression that combines them:

 

stmt = relational statement such as (a < b)

T = True

F = False

 

            Logical and                                                                 Logical or

stmt1 stmt2 (stmt1 && stmt2)              stmt1 stmt2 (stmt1 || stmt2)

   T     T         T                          T     T       T

   T     F         F                          T     F       T

   F     T         F                          F     T       T

   F     F         F                          F     F       F

 

Logical negation

stmt  !stmt

 T      F

 F      T

 

A common use of logical expressions is to check whether a value is within or outside of a range.  Suppose we’re to write a program that displays a statement if the value of a variable num is within the range 5-10.  For num to be in the range means that two conditions must be true: 1) its value is greater than or equal to 5,  2) and its value is less than or equal to 10.  Here’s a code segment that expresses this condition:

 

      Scanner input = new Scanner(System.in);

      System.out.print(“Enter a value: “);

      int num = input.nextInt();

      if ((num >= 5) && (num <=10))

            System.out.println(num + “ is in the range 5-10.”);

 

On the other hand, expressing the condition that num is outside the range 5-10 requires that either num is less than 5 or num is greater than 10.  The corresponding if statement would be:

 

      if ((num < 5) || (num > 10))

            System.out.println(num + “ is outside the range 5-10.”)

 

It may have already dawned on you that this is not the only logical expression that can be used to express the condition that a value is outside of a range.  If the expression ((num >= 5) && (num <=10)) refers to a value being in the range, its negation  !((num >= 5) && (num <=10)) must refer to it being outside the range (notice the ! symbol).  The two expressions

 

            ((num < 5) || (num > 10))  and    !((num >= 5) && (num <=10)) 

 

then are logically equivalent.  Which one you use is a matter of preference, and here are some logical equivalences to help you reason through expressions more easily:

 

            !(a < b)                is equivalent to     (a >= b)

      !(a != b)               is equivalent to     (a == b)

      !((a < b) && (x >= y))  is equivalent to     ((a >= b) || (x < y))

      !((a == b) || (x > y))  is equivalent to          ((a!=b) && (x <= y))

 

In the last two expressions, we distributed the negation operator to all the other operators: < becomes >=, != becomes ==, && becomes !! and vice versa.  Some of these equivalences are attributed to a 19th century mathematician, Augustus De Morgan, and are referred to as De Morgan’s law (something for a Web search).