Java If Else Statement: Mastering the Flow of Execution

javaifelseWhen you first begin programming in Java, most of your programs are sequential, or linear. This means that the code is executed from the top to the bottom and every single line of code is read. As your programs become more advanced, you will not always want the flow of execution to be sequential. You may want to skip over certain statements depending on whether or not certain conditions are met. This is known as conditional logic and Java makes it very easy to add these statements into your programs.

One of the easiest conditional statements in Java is known as the If statement. In this tutorial, you will learn how to construct If statements and ultimately, If-Else statements. Java Fundamentals I & II provides an excellent overview of conditional logic for Java.

The If Statement

The If statement allows code to be executed when something happens that you specify. The basic structure of the If statement in Java looks like this:

If (Statement) {

}

Within the brackets, you can add an action that you would like your program to perform if the conditions of the If statement are met. For instance, if you want to check whether a user is over the age of 18, you could create an If statement that looks like this:

If (userAge < 18) {

}

The above statement simply checks to see if the user is under the age of 18. At this point, the statement does nothing else and execution would continue because there is no executable statement within the brackets. The user variable could get its value from any number of places including direct input from the user or from a database in more advanced applications. Either way, the next step is to make the program do something when it detects that a user is under the age of 18. That Java statement might look like this:

If (userAge < 18) {

        System.out.println (“You are under the age of 18.”);

}

In this case, if the program detects that the user is under the age of 18, a message is displayed in the console that says “You are under the age of 18.” The reality is that this statement could do any number of things if the conditions in the If statement are met.

Using the same example, if the user were over the age of 18, this entire If statement would be skipped over and execution would continue at the next line of code after the statement.

This example is fine, however, what if you want your program to react when either the user is under 18 or they are over 18? Using only If statements, you can create an output for either situation. The code for that program would look like this:

Public static void main(String[ ] args) {

        Int userAge = 18;

        If (userAge <= 18) {

                System.out.println(“User is 18 or younger”);

        }

        If (userAge > 18) {

                System.out.println(“User is older than 18”);

        }

}

In this example, the output would be “User is 18 or younger.” The point is that no matter what the user’s age is, the program will react and output a string value either way. You can learn more about using If statements in the Introduction to Java Training Course.

This can also be accomplished by using the If-Else statement.

The If-Else Statement

Instead of using two If statements, you can use the If-Else statement instead. The structure of the If-Else statement looks like this:

If (condition to test) {

}

Else {

}

Just like the If statement, the first line starts with If and is followed by the condition you want your program to test for. Within the curly brackets you would include the actions you want the program to take if that condition is met. If the condition is not met, the program executes the commands within the else statement instead.

Using the example above to check the age of the user, the If-Else statement would be constructed like this:

Public static void main(String[ ] args) {

        Int userAge = 18;

        If (userAge <= 18) {

                    System.out.println(“User is 18 or younger”);

        }

        Else {

                    System.out.println(“User is older than 18”);

        }

}

This code accomplishes the same goal as the double If statements used in the example above, but uses a single statement that executes one of the two statements depending on the value of the user variable. In the case of this example, the output would be “User is 18 or younger.”

These are simplified examples that show how to use the If-Else statement effectively. Another popular conditional statement used in Java programs is the If-Else If statement. This allows your program to test for more than two choices in a single If-Else statement. In fact, an If-Else If statement can check for as many choices as you want. The syntax of this statement looks like:

If (condition 1) {

}

Else if (condition 2) {

}

Else {

}

Realistically, this statement could continue on by adding as many Else If conditions as you want. However, if you have more than of few conditions to test for, you are probably better off using the Java switch statement instead.

You can learn more about conditional logic including If-Else statements in the Introduction to Java Programming for Online Learners.

Learning how to incorporate conditional statements into your Java programs is what allows them to function seemingly autonomously. Properly constructed If statements and If-Else statements allow your program to make choices and execute depending on user input or other outside information.

Although there are certainly reasons to use strictly sequential applications, you will find that most programs require some degree of manipulation in the flow of execution to be deemed efficient and useful. Believe it or not, conditional logic statements are one of the most basic, yet powerful, tools available to you as a Java programmer.  Java for Absolute Beginners provides a solid foundation in Java and conditional statements if you are looking for further information.

Practice using these examples and change around the numbers to learn how your changes affect the output of the program. Like just about anything else in the programming world, practice is the best way to fully understand these concepts and begin incorporating them into your very own Java programs.