Java Control Structures - If Statement

March 26, 2018 Oclemy Java Control Structures 3 minutes, 6 seconds

An if statement is a prgramming construct that allows a program to specify alternatie paths of execution.

Categories of If Statements

If statements can be divided into the following categories:

  1. One-way if statement.
  2. Two-way if-else statement.
  3. Nested if statements.
  4. Multi-way if-else statements.

Furthermore we also have switch and conditional expressions as selection statements.

One-Way If Statement

This executes an action if and only if the specified expression evalutaes to true.

Here's its syntax:

if(condition)
{
    do_action();
}

Boolean expressions do evaluate to true or false. The do_action() method in the case above will only be executed if the result is true.

package info.tutorialsloop;

public class Main {

    public static void main(String[] args) {

        int favoriteNumber = (int) (System.currentTimeMillis()%10);
        if(favoriteNumber > 5)
        {
            System.out.println("My favorite number is greater than 5");
        }
        System.out.println("The number is "+favoriteNumber);
    }
}

RESULT

My favorite number is greater than 5
The number is 8

Another time if you run you get this:

The number is 0

Clearly you can see in the first situation 8 > 5 so the expression evaluates to true. Hence the My favorite number... is printed out.

However in the second situation, 0 > 5 evaluates to false. Hence it's not executed.

Two-Way If-Else Statements

An two-way if-else statement or an if-else statement decides execution path based on whether the condition evaluates to true or false.

The one-way if statement we'd looked at earlier performs an action if and only if the specified condition evaluates to true. Otherwise the statement ends with nothing done.

However, an if-else statement allows for alternatie actions to be performed when the condtion is false.

Here's the syntax:

if(condition){
    execute_plan_A();
}else{
    execute_plan_B();
}

Here's an example that generates a random number and checks whether it's greater than or less than 5. And prints our a message based on that.

package info.tutorialsloop;

public class Main {

    public static void main(String[] args) {

        int favoriteNumber = (int) (System.currentTimeMillis()%10);
        if(favoriteNumber > 5)
        {
            System.out.println("My favorite number ,"+favoriteNumber+" is GREATER than 5");
        }else{
            System.out.println("My favorite number ,"+favoriteNumber+" is LESS than 5");
        }
    }
}

RESULT

My favorite number ,3 is LESS than 5

Nested If and Multi-Way If-Else Statements

A nested if statement is an if statement that is inside another if statement. This can be an if or if-else statement.

The inner if statement is said to nested inside the outer one. You can nest your if statements without any limit.

Here's the syntax.

if(fuelAvailable)
{
    if(startEngineSuccessful)
    {
        if(liftOffSpeedAttained)
        {

        }
    }
}else{
    if(fuelOrderMade)
    {
        .....
    }
}

In this case we have an outer two-way if else statement. It checks for fuel availability.

If fuel is available then its attempts to start the engine. If the start engine is successful we attempt to reach the launch speed. So as you can see we are nesting the conditions inside of each other.

If fuel is not available then we check if an order has been made.Once more we are nesting an if statement inside the else part of the outer statement.

Heres an example:

package info.tutorialsloop;

public class Main {

    public static void main(String[] args) {

        int favoriteNumber = (int) (System.currentTimeMillis()%10);
        if(favoriteNumber > 5)
        {
            System.out.println("My favorite number ,"+favoriteNumber+" is GREATER than 5");

            if (favoriteNumber % 2 == 0)
           {
               System.out.println("My favorite number is an EVEN NUMBER");
           }else{
                System.out.println("My favorite number is an ODD NUMBER");
            }
        }else{
            System.out.println("My favorite number ,"+favoriteNumber+" is LESS than 5");
            if (favoriteNumber % 2 == 0)
            {
                System.out.println("My favorite number is an EVEN NUMBER");
            }else{
                System.out.println("My favorite number is an ODD NUMBER");
            }
        }
    }
}

RESULT

My favorite number ,7 is GREATER than 5
My favorite number is an ODD NUMBER

Another time if you run it you can get something like this:

My favorite number ,2 is LESS than 5
My favorite number is an EVEN NUMBER

Comments