Java Image Header

Learn Java 5: Conditions part 1

This is the fourth official article in the Java coding series. The previous article, Learn Java 4: Project - BasicMath, can be found at the following link:

Now that we know how to write very simple programs, you may be wondering, “How do I only do something based on some outcome of an event?”. That is the job of conditions. In not only Java, but programming in general, conditions are used to strategically control whether to do something or not, based on whether they are true or false.

Before looking at how to use conditions in Java, it is important to understand the fundamental logic behind them. For example, consider the English sentence:

If I’m hungry tonight, I’ll go to the store and buy food. Otherwise, I’ll go to sleep.

This is a conditional statement in English; it controls what the person will do based on how they feel. This is also applicable to programming, as seen in the following pseudocode:

If x is even, divide it by 2. Otherwise, add 1 to x.

Conditions can even exist without the “otherwise” section:

If x is even, divide it by 2. Otherwise, do nothing.

Thus, these types of statements follow the same basic formula:

If something is true, do something. Otherwise, do something (or nothing).

In programming, this is accomplished through an if statement. Just as in the examples, if statements allow you to do something based on a condition, or something else if the condition is violated. In Java, if statements follow the following syntax:

if (condition) {
// Do something
else {
// Do something else

You may now be wondering “What if I want to test 2 conditions in succession?” Well, there is another step to if statements, known as an else if statement. Just in sentence form, this looks something like this:

If I’m hungry, I’ll go to the store and buy something to eat. Otherwise, if I’m thirsty, I’ll go to the store and buy something to drink. Otherwise, I’ll stay home.

In Java, this is very similar to an if statement:

if (isHungry()) {
else if (isThirsty()) {

This grouping of if’s, else if’s, and else’s is known as a chain of conditions (not officially, but commonly). Each chain of conditions must have at least an if statement (only one), and can also have as many else if statements as you need. Finally, it can have an else statement, if necessary, but only one. Thus, chains of conditions are identified by the if statement, which is always the first condition. The else if’s follow, and the chain is concluded by the else statement.

Finally, there is one more case that conditions cover: what if I’m either hungry, thirsty, or potentially both? In Java, as well as in many other languages, testing chains of conditions begins with the if statement at the beginning, moving down the chain as it goes, if the current condition is false. If the condition being tested is true, the conditions are no longer tested. Instead, Java runs the code inside of the statement with the true condition. Afterward, it skips the rest of the chain, moving below to the next lines of code outside of the chain. Thus, the above chain of conditions can only handle when I’m either hungry or thirsty, but not both, because if I’m hungry, it skips over the isThirsty() condition. For this, we would need two separate chains, each with one condition:

if (isHungry()) {
if (isThirsty()) {

Now, when our hypothetical code is run, Java tests the first condition, to see if I’m hungry. If it is true, I buy food. Next, it moves on to the next chain, rather than skipping the second condition as it did above, and tests if I am thirsty. If I am, meaning that the condition is true, I buy something to drink. This format is very useful if you have multiple conditions test, and different outcomes for each.

Stay tuned for the next tutorial, conditions part 2, where we explore more ways to manipulate and combine conditions for even more flexibility.

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.