Java Image Header

Learn Java 6: Conditions Part 2

This is the fourth official article in the Java coding series. The previous article, Learn Java 5: Conditions Part 1, can be found at the following link:

In the previous article, we learned about if statements and conditions, and how to test for an outcome to control the flow of our program. However, we only learned to test one condition per statement, but situations are often more complicated than this. For this reason, we have different ways to structure our conditions, as well as several logical operators, to help us add this functionality.

The first situation that requires additional functionality is when you have two conditions that both must be true:

If I’m hungry and I have money, I’ll go buy something to eat.

As you can see, this is not a simple, straightforward if-then condition; it requires that multiple conditions be true. However, our knowledge gained from the previous tutorial does not teach us how to handle this, so we need new concepts. There are two different possible approaches to this problem, each with its own strengths and weaknesses.

Firstly, you could use nested if statements. The word nested means that there is one inside of the other, so evidently nested if statements are if statements inside of each other. This ensures that, if the first condition is true, Java moves inside of the if statement to execute the code. This will bring it to the second if statement, which is inside of the first. Then, all you need to do is write your “outcome code” inside of the second if statement, and your dual conditions will work; the code inside of the second if statement will only execute if both of the conditions are true. In Java, this would look something like this:

if (isHungry()) {
if (hasMoney()) {

An alternative to this approach would be to use a logical operator. Logical operators are symbols that represent ‘actions’ in logic. There are three logical operators, ‘and’, ‘or’, and ‘not’. Although their names are somewhat self-explanatory, here’s an overview of what they do: and “chains” two or more conditions together, requiring that they all be true for the whole condition to be true. Or chains conditions together differently, as long as at least one of the connected conditions are true, the entire condition will be true. Not negates a condition, so if the condition that the not applies to evaluates to true, the not makes it false, and vice versa.

In this case, the obvious choice of logical operator is the ‘and’ operator, because we want to chain two conditions together such that they both must be true for the condition to be true (I must both be hungry and have money). In Java, the and operator is symbolized with a ‘&&”; using it would look something like this:

if (isHungry() && hasMoney()) {

The or operator, symbolized by a ‘||’ in Java (the ‘|’ character is the pipe, usually the capital character of the backslash key). Using it is much the same as the and character. However, the not operator functions differently than the other two; it applies to one condition and negates its outcome. It is symbolized by the exclamation mark, sometimes called the bang. Here is an example of the not operator in Java:

if (!hasMoney()) {

As you can see, if I do not have money, then I need to get a job. The not operator can also be applied to a chain of conditions:

if (!(hasMoney() && hasJob()) {

In this slightly expanded version of the last condition, you can see that the not operator applies to the whole condition created by the and operator.

One important thing to note when Java evaluates chains of conditions is the order in which the conditions are checked. Java evaluates conditions using short circuit evaluation, meaning that they take the shortest path through the condition when possible. Thus, for long conditions chained together by logical operators, Java only evaluates enough conditions to know the answer for sure:

if (x > 5 && x % 2 == 0) {
x -= 10;

As you can see, the and operator makes it only necessary for one of the individual conditions to be false for the entire condition to be false. Java knows this, so if it finds that the first condition is false, it will not even bother to check the second condition, because the answer to the entire condition is false no matter the outcome of the second. The same is true for the or operator; if the first condition is true, Java does not check the second condition because the entire condition is always true. This method of executing conditions is very useful to programmers; it allows us to put a potentially illegal condition (one that would cause an error in our program) as the second condition of a chain, and put a condition in the first part that ensures that the second will not cause an error if the first one is true. For example:

if (y > 5 && x / y > 1) {
System.out.println(“x must be greater than 5”);

Now that you have a fundamental understanding of conditions, we will move on to applying them to other new programming structures, in order to expand the functionality of your programs even further.

The links to our previous articles in this series are:

Tagged in : Computer Science

Arthur Lafrance Image

Arthur Lafrance

Arthur Lafrance is a junior at Homestead High School. Having extensive experience with computer programming, he seeks to educate others about coding and technology. He plans to study computer science and engineering in college. He is the NYTJ Director of Branding.