The If Statement Tutorial

A statement in Java is a complete unit of execution terminated with a semicolon. A statement can be as simple as a unit of execution like int a = 100; or System.out.println("Hello World!");. Statements inside of a method are executed in a top-down order. What if you want to change the order of how statements are executed, or even prevent statements from being executing based on a certain condition? Java provides a set of control flow statements to perform that very task. The most basic of the control flow statements is the if statement.



The if statement structure

The if statement evaluates a condition to determine if the result is either true or false. The conditional test is contained inside the set of parenthesis located directly after the Java keyword if. If the condition evaluates to be true then the statement(s) inside the code block are executed. The code block braces are optional for execution of just a single statement.

if (condition) {
    Statements are executed if condition equals true.
}


Now what if we want another statement or statements to execute based on the condition evaluating to false? That is where the else Java keyword comes into play.

if (condition) {
    Statements are executed here if condition equals true.
}
else {
    Statements are executed here if condition equals false.
}


The conditional test can be performed using one of the Java relational operators such as:
== equal to
> greater than
>= greater than or equal to
< less than
<= less than or equal to
!= not equal to


Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md IfStatement
C:\Java>cd IfStatement
C:\Java\IfStatement>Notepad IfStatement.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


class IfStatement {
    public static void main(String args[]) {
        int currentSpeed; //declaration statement
        int speedLimit; //declaration statement
        int speedDifference; //declaration statement
        
        currentSpeed = 72; //assignment statement
        speedLimit = 65; //assignment statement
        
        System.out.println("The speed limit is "+speedLimit+", you are traveling "+currentSpeed);

        if (currentSpeed == 65) {
            System.out.println("You are traveling at exactly the speed limit.");
            System.out.println("Good driving.");
        } 

        if (currentSpeed > 65) {
            System.out.println("You are traveling over the speed limit.");
            speedDifference = currentSpeed - speedLimit;

            // let's display different messages based on the severity of the speeding.
            if(speedDifference >= 5) { 
                System.out.println("Better slow down, you are at risk for getting a speeding citation.");
            } 
            else { 
                System.out.println("Minor speeding - just like most other drivers on the road.");
            }
        } 

        if (currentSpeed < 65) {
            System.out.println("You are traveling under the speed limit.");
        } 

        System.out.println();
        System.out.println("-------------");
        System.out.println("if-else if-else");

        // we can perform essentially the same thing by using if-"else if"-else syntax.
        
        if (currentSpeed == 65) {
            System.out.println("You are traveling at exactly the speed limit.");
            System.out.println("Good driving.");
        } 
        else if (currentSpeed > 65) {
            System.out.println("You are traveling over the speed limit.");
        } 
        else {
            System.out.println("You are traveling under the speed limit.");
        }

        System.out.println();
        System.out.println("-------------");
        System.out.println("optional code block syntax - curly opening and closing braces");

        // code blocks indicated by a pair of curly braces are optional 
        // only if you are only going to execute a single statement after the if statement.
        
        if (currentSpeed == 65)
            System.out.println("You are traveling at exactly the speed limit.");

        if (currentSpeed > 65) 
            System.out.println("You are traveling over the speed limit.");

        if (currentSpeed < 65) System.out.println("You are traveling under the speed limit.");

        if (currentSpeed > 65) 
            System.out.println("You are speeding.");
        else
            System.out.println("You are not speeding.");
    }
}

Now switch back to the command prompt (CMD) and type in javac IfStatement.java and press Enter.
Now type in java IfStatement and press Enter.


C:\Java\IfStatement>javac IfStatement.java
C:\Java\IfStatement>java IfStatement
The speed limit is 65, you are traveling 72
You are traveling over the speed limit.
Better slow down, you are at risk for getting a speeding citation.

-------------
if-else if-else
You are traveling over the speed limit.

-------------
optional code block syntax - curly opening and closing braces
You are traveling over the speed limit.


Final thoughts

Play around with changing the values of the currentSpeed variable. Change currentSpeed to 48, 65, and 68. See what happens.


Tutorials