### Experiment 5: Logical Expressions

It is sometimes useful to be able to combine relational expressions. For example, suppose we wish to find out if the value of x is within the range 1 to 100. Mathematically, you can write

```   1 <= x <= 100
```

However, Java is not Mathematics: if you try this the compiler will generate code to

1. Evaluate the left relational subexpression
```      1 <= x
```

producing a boolean value

2. Java tries to use that boolean value as the left-operand of a relational subexpression, but <= does not work with boolean values and so the compiler generates an error -- which is not what we intended!

Using Express.java, try it and see for yourself!

To do this sort of thing in Java, what is needed is to join the two relational expressions

```   1 <= x
```

and

```   x <= 100
```

using one of the logical operators. These operators include:
 && AND, produces true if and only if both of its operands are true. || OR, produces true if and only if either of its operands are true.

The AND and OR operators each require two operands. A third logical operator is the NOT operator (!), which is applied to just one relational operand (i.e., !(1 <= x)) and produces true if that expression is false, and produces false if that expression is true.

To solve the problem mentioned above, we can use the AND operator:

```   1 <= x && x <= 100
```

For this problem, we could have instead used the OR and NOT operators:

```   !(1 > x || x > 100)
```

and still achieved the correct result. Try one of these in Express.java, and verify that it gives you the correct result for both positive and negative values.

Now it is your turn: in the space below, write a logical expression using the AND operator that produces the value true if the value of char variable ch is uppercase (i.e., within the range 'A' to 'Z', and produces false otherwise. Test that it is correct using Express.java.

Then rewrite your logical expression using the OR operator (and different relational operators) so that it produces the same results. (Hint: Use the NOT operator.) As before, test that it is correct using Express.java

Relational expressions and logical expressions both produce boolean values as their result. For this reason, such expressions are collectively referred to as boolean expressions.

Boolean expressions have a variety of uses. For example, suppose we have a problem that requires us to input a number x, and the nature of the problem is such that the number must be nonzero, or we will be unable to solve the problem correctly (perhaps the number will be used in a division). This requirement that the number must be nonzero is called a precondition. We can check that such a precondition is true by evaluating the expression

```   x != 0
```

If it is false, we will display a diagnostic message and then stop the program. If it is true, we will continue on in the execution.

You should now know about the logical operators, and how they can be used to combine relational expressions into arbitrarily complex boolean expressions. You should also know about how preconditions can be expressed as boolean expressions.

Back to the Exercise List

Forward to the Next Experiment