Java - If Else statement

Java

In this tutorial we will learn about if statement in Java programming language.

The if statement is a decision making statement and it helps us to control the flow of the execution of the program.

If syntax

We use the if statement to execute a block of code only if certain condition is satisfied.

Following is the syntax of the if statement.

if (expression) {
  // some code...
}

If the expression evaluates to true then the body of the if statement is executed. Otherwise, it is ignored.

Example #1: Write a program in Java to print "Awesome" if given condition is satisfied

In the following example we will print "Awesome" depending on the value of the variable isGameOver.

class Example {
  public static void main(String args[]) {
    boolean isGameOver = false;

    if (isGameOver != true) {
      System.out.println("Awesome");
    }

    System.out.println("End of program.");
  }
}

Output:

Awesome
End of program.

The above code prints "Awesome" because the condition isGameOver != true is satisfied.

If-Else statement

We use the if-else statement to execute one of the two blocks of code. If the given condition is satisfied then the code of the if-block is executed. Otherwise, code inside the else-block is executed.

Following is the syntax of the if-else statement.

if (expression) {
  // some code...
} else {
  // some other code...
}

If the expression is satisfied then the code inside the if-block is executed. Otherwise, code inside the else-block is executed.

Example #2: Write a program in Java to print "Awesome" if given condition is satisfied else print "No!!!"

In the following example we will print "Awesome" depending on the value of the variable isGameOver.

class Example {
  public static void main(String args[]) {
    boolean isGameOver = true;

    if (isGameOver != true) {
      System.out.println("Awesome");
    } else {
      System.out.println("No!!!");
    }

    System.out.println("End of program.");
  }
}

Output:

No!!!
End of program.

The above code prints "No!!!" because the condition isGameOver != true is not satisfied.

Else-If statement

If we want to have multiple conditions then we use else-if statement.

Following is the syntax of the else-if statement.

if (expression_1) {
  // some code...
} else if (expression_2) {
  // some code...
} else if (expression_3) {
  // some code...
} else {
  // some other code...
}

In this case we check which of the given condition is valid and when we find that match then we execute that if-block code. If no condition is satisfied then we execute the code inside the else-block.

Example #3: Write a program in Java to print whether a number is odd or even or zero

In the following example we will print "Even number" if the number is even, "Odd number" if the number is odd and "It's zero" if the number is zero.

Note! If a number is divisible by 2 then it is an even number.

So, to check even number we will take help of the Modulus operator %.

class Example {
  public static void main(String args[]) {
    int num = 3;

    if (num == 0) {
      System.out.println("It's zero");
    } else if (num % 2 == 0) {
      System.out.println("Even number");
    } else {
      System.out.println("Odd number");
    }

    System.out.println("End of program.");
  }
}

Output:

Odd number
End of program.