Combining conditions: logical operators

So far, we've been using if and else with "simple" conditions. But what if we want to combine conditions? For example, we might want to give the player an extra card if there are cards left in the pack and the player has less than 5 cards in their hand. To do this, we use what are sometimes called logical operators: things like "and", "or", "not".


The and operator looks as follows:

if (cardsLeftInPack > 0 && cardsInHand < 5) {

Note that we put two "and" symbols (&&).


To check for one condition or the other, we use two pipe symbols. This is the vertical line that on many keyboards is obtained by holding down SHIFT and pressing the button to the left of the Z key. (On some other common layouts, you hold down CTRL and SHIFT and press the 1 key.) The code then looks as follows:

if (timesTableNo != 10 || n >= 5) {
  System.out.println(timesTableNo + " x " + n + " ...);

Note that or means if either of the conditions is true, or if both conditions are true1. So this code would print out the line if the times table number was not 10, or if n was at least 5. In other words, it would print out the whole times table in the case of times tables other than 10, but for the 10 times table, would print out the table from 5x10 onwards.


We can use the Java else statement to add some code to be executed when a given condition isn't met. But we can also write a condition that means "if not ...". The Java symbol for not in this case is the exclamation mark !. So if we want to write "if it isn't true that there are cards left in the pack and the player has fewer than 5 cards", then we can write:

if (!(cardsLeftInPack > 0 && cardsInHand < 5)) {
  System.out.println("You can't have more cards!");

Notice how in this case, we put extra brackets around the whole condition that we want to negate. In reality, it's often possible to avoid using !. For example, in this case, we could have written:

if (cardsLeftInPack == 0 || cardsInHand >= 5)) {
  System.out.println("You can't have more cards!");

Arguably, this version is a bit clearer: just as people generally have trouble interpreting sentences with too many "nots" in them2, in programming, people appear to find it harder to interpret conditions with nots in them versus equivalent conditions without them.

Next: arrays

On the next page, we look at arrays in Java, which allow us to deal with a "row of pigeon holes" rather than singly labelled variables.

1. If you need the notion of either but not both, there exists the "hat" operator ^, but in reality it is rarely used.
2. People often have trouble deciding between sentences such as No program is too trivial to debug vs No program is too trivial not to debug. In other words, we often have trouble "getting the right combination of nots". In human language, we can cope with this illogicality most of the time, but in computer programming, your lines of code will be interpreted literally!

If you enjoy this Java programming article, please share with friends and colleagues. Follow the author on Twitter for the latest news and rants.

Editorial page content written by Neil Coffey. Copyright © Javamex UK 2021. All rights reserved.