Without a doubt, expressions are the main building blocks of a program. This is because there are so many different kinds of expressions that a majority of the source-code lines in a program end up being-you guessed it-expressions. There are expressions that result in numerical values. There are expressions that result in strings. There are simple expressions, complex expressions, and all manner of expressions in between.
In this post you'll learn not only more about those types of expressions, but also about logical expressions, which help a computer seem to be able to think and make choices. Along the way, you'll discover comparison and logical operators, which make logical expressions possible.
Types of Expressions
To put it simply, an expression is a line of code that can be reduced to a value or that assigns a value. For example, you know that the addition operator adds one expression to another, like this:sum = expr1 + expr2;
In the preceding line, expr1 can be something as simple as the variable x or as complex as (4 + 5) * 2 * 5 / 7 + x / y. The same goes for expr2, of course. And, in fact, the first example containing expr1 and expr2 is an expression itself!
But no matter how complicated, all expressions can be classified into one of three main categories:
- Numerical expressions combine numbers, variables, or constants using mathematical operators. An nexample is 2 + 3 / x.
- Assignment expressions assign a value to a variable. An example is num = 3.
- Logical expressions are unique in that they result in a value of true or false. An example is x
Expressions Within Expressions
Look at the following assignment expression:num = (5 - x) * (2 + y);
This is an assignment expression because it assigns a value to the variable num. However, the stuff on either side of the equals sign contains these other expressions:num
(5 - x) * (2 + y)
Both of the above lines are numerical expressions because they can be reduced to a numerical value (assuming that you know the values of num, x, and y.
But, wait a second-you're not done yet. You can still find more sub-expressions. Look at the multiplication operation. Can you see that it's multiplying two expressions together? Those two expressions look like this:(5 - x)
(2 + y)
And the above simplified expressions contain yet more sub-expressions. Those expressions are:5
Expressions are what programmers like to call recursive, meaning that the definition of an expression keeps coming back on itself. An expression contains expressions that contain other expressions, which themselves contain other expressions. How deep you can dig depends on the complexity of the original expression. But, as you saw demonstrated, even the relatively simple expression
num = (5 - x) *
(2 + y) has four levels of depth.
Now that you've dug into the secrets of expressions, it's time to learn about a new type of operator. So far, you've gotten some practice with mathematical operators, which enable you to build various types of numerical and assignment expressions. Another type of operator you can use to build expressions is the comparison operator. Comparison operators are used to create logical expressions, which, if you recall, result in a value of true or false. Table 1 lists the logical expressions used in Java programming. C and C++ programmers will find these operators very familiar.
Table 1 Java's Logical Operators.
Operators - Description
== - Equal to
> - Greater than
>= - Greater than or equal to
!= - Not equal to
Example: Using Comparison Operators
Just how do you use comparison operators? As their name suggests, you use them to compare two expressions, with the result of the comparison being either true or false. For example, look at this logical expression:3 == 2 + 1
The result of the above expression is true because the == operator determines whether the expressions on either side are equal to each other. If you were to change the expression to 3 == 2 + 2 the result would be false. That is, 3 does not equal 4. However, the previous sentence suggests a way to rewrite the expression, like this:3 != 2 + 2
This expression results in a value of true, because 3 does not equal 4. The other logical expressions work similarly. Table 2 lists a number of logical expressions and the results they produce.
Table 2 Examples of Logical Expressions.
Expression - Result
3 + 4 == 7 - true
3 + 4 != 7 - false
3 + 4 != 2 + 6 - true
3 + 4
3 + 4
3 + 4 == 4 + 4 - false
3 + 4 > 10 - false
3 + 4 >= 7 - true
3 + 4 >= 8 - false
The comparison operators enable you to compare two expressions. But another type of operator-logical operators-supercharges comparison operators so that you can combine two or more logical expressions into a more complex logical expression. Even if you've never programmed a computer before, you're already familiar with logical operators because you use them in everyday speech. For example, when you say, "Do you have a credit card or ten dollars in cash?" you're using the logical operator OR. Similarly, when you say, "I have a dog and a cat," you're using the AND operator. Table 3 lists Java's logical operators and what they mean.
Table 3 Java's Logical Operators.
Operator - Description
&& - AND
|| - OR
^ - Exclusive OR
! - NOT
The AND (&&) operator requires all expressions to be true for the entire expression to be true. For example, the expression(3 + 2 == 5) && (6 + 2 == 8)
is true because the expressions on both sides of the && are true. However, the expression(4 + 3 == 9) && (3 + 3 == 6)
is false because the expression on the left of the && is not true. Remember this when combining expressions with AND: If any expression is false, the entire expression is false.
The OR operator (||) requires only one expression to be true for the entire expression to be true. For example, the expressions(3 + 6 == 2) || (4 + 4 == 8)
and(4 + 1 == 5) || (7 + 2 == 9)
are both true because at least one of the expressions being compared is true. Notice that in the second case both expressions being compared are true, which also makes an OR expression true. The exclusive OR operator (^) is used to determine if one and only one of the expressions being compared is true. Unlike a regular OR, with an exclusive OR, if both expressions are true, the result is false (weird, huh?). For example, the expression(5 + 7 == 12) ^ (4 + 3 == 8)
evaluates to true, whereas these expressions evaluate to false:(5 + 7 == 12) ^ (4 + 3 == 7)
(5 + 7 == 10) ^ (4 + 3 == 6)
The NOT (!) operator switches the value of (or negates) a logical expression. For example, the expression(4 + 3 == 5)
is false; however, the expression!(4 + 3 == 5)
Example: Using Logical Operators
Take a look at the following expression:(4 + 5 == 9) && !(3 + 1 = 3)
Is this expression true or false? If you said true, you understand the way the logical operators work. The expressions on either side of the && are both true, so the entire expression is true. If you said false, you must go to bed without any dinner.
Example: Using Multiple Logical Operators
Just as with mathematical operators, you can use multiple logical operators to compare several logical expressions. For example, look at this expression:(4 == 4) && (5 == 5) && (6 == 6)
This expression gives a result of true because each expression to the left and right of each AND operator is true. However, this expression yields a value of false:(4 == 4) && (5 == 6) && (6 == 6)
Remember that, when using AND, if any sub-expression is false, the entire expression is false. This is kind of like testifying in court. To be true, it's got to be the truth, the whole truth, and nothing but the truth.
Example: Combining Different Comparison and Logical Operators
Again, just like mathematical operators, there's no restriction on how you can combine the different comparison and logical operators, although if you build a very complex expression, you may have trouble evaluating it yourself. Check out this expression:(3 6)
Here you've used four different comparison and logical operators in the same complex expression. But because you're comparing the sub-expressions with the AND operator, and because each of the sub-expressions is true, the result of the above expression is true. Now, look at this expression:((3 < 5) && (2 == 1)) || (7 == 7)
Yep, things are getting tricky. Is the above expression true or false? (Hey, give it a shot. You've got a fifty-fifty chance.) Ready for the answer? The above expression is true. First, look at the parentheses. The outermost parentheses, on the left, group the two expressions being compared by the AND operator into a single expression, so evaluate it first. The value 3 is less than 5, but 2 does not equal 1, so the entire expression on the left of the OR operator is false. On the right of the OR operator, however, 7 does indeed equal 7, so this sub-expression is true. Because one of the expressions in the OR comparison is true, the entire expression is true. Here's how the expression breaks down, step-by-step:((3 ((true) && (false)) || (7 == 7)
false || (7 == 7)
false || true
Writing Logical Expressions
You wouldn't write expressions such as(4 + 5 == 9) && !(3 + 1 == 3)
in your programs. They would serve no purpose because you already know how the expressions evaluate. However, when you use variables, you have no way of knowing in advance how an expression may evaluate. For example, is the expression(num 15)
true or false? You don't know without being told the value of the numerical variable num. By using logical operators, though, your program can do the evaluation, and, based on the result-true or false-take the appropriate action. In the next chapter, which is about if and switch statements, you'll see how your programs can use logical expressions to make decisions.
Order of Operations
Like all operators, comparison and logical operators have an order of operations, or operator precedence. When you evaluate a complex expression, you must be sure to evaluate any sub-expressions in the correct order. As you learned in the previous example, however, you can use parentheses to group expressions so that they're easier to understand or to change the order of operations. Table 4 lists the comparison and logical operators in order of precedence.
Table 4 Comparison and Logical Operators' Order of Operations.
Operators - Description
! - NOT
= - Relational
== != - Equality
^ - Exclusive OR
&& - Logical AND
|| - Logical OR