HOLIDAY SALE! Save 50% on Membership with code HOLIDAY50. Save 15% on Mentorship with code HOLIDAY15.

7) Conditionals & Loops Lesson

Java If & If/Else Statements

6 min to complete · By Ryan Desmond

The if statement is a fundamental control statement in programming. It is used to determine whether or not a program should execute a sequence of code.

How Does the if Statement Work in Java?

In Java, the if statement syntax looks like this:

if (boolean condition) {  
  // if the boolean condition resolves to TRUE  
  // the code within the IF block will be executed
}

Java if Statement Example

The following code prints a message ONLY if the variable i equals 100.

class Main {
  public static void main(String[] args){
    int i = 100;

    if (i == 100){  
      System.out.println("The value of i is 100");  
    }

    i = 99;
    if (i == 100){  
      System.out.println("Does this print??");  
    }
  }
}

The Java if statement is only executed when the boolean condition evaluates to true.

You Can Nest if Statements

In programming, nesting is the process of putting something inside of itself. Therefore, nesting an if statement means putting an if statement inside an if statement.

In Java, you can nest if statements as many times as you want. For example:

class Main {
  public static void main(String[] args){
    int i = 10;
    
    if (i > 0){  
      if (i < 20) {  
        System.out.println("i is > zero and < 20");
        // you could keep nesting if statements 
        // here if you want/need
      }
    }
  }
}

The else Statement

In Java, you can create a flow in your code by linking multiple if statements with the else if statement and ending the chain with the else statement.

This type of conditional ladder allows a program to check several cases, and if none of the cases are satisfied, the else clause is executed. An example:

class Main {
  public static void main(String[] args){
    int i = 0;
    if (i > 0) {  
      // i is greater than zero
      System.out.println("i is greater than zero");
    } else if (i < 0){  
      // i is less than zero
      System.out.println("i is less than zero");
    } else {  
      // i is equal to zero
      System.out.println("i is equal to zero");  
    }  
  }
}

Notably, the ladder of if, else if, and else statements are sequential and only run if the previous statement returned false. In other words, once one of the statements is true, then only the code inside that code block will be run and no other.

Experiment with If/Else Statements

Use the code editor below to demonstrate your knowledge of if statements and if/else-if ladders.

class Main {
  public static void main(String[] args) {
    // please write and run at least 3 unique 
    // if statements below
    int i = 10;
    if (i < 20){
      System.out.println("i is less than 20!");
    }


    // additionally, please write and run at least 
    // two unique if/else-if/else ladders below
  



    // keep your code above this line
  }
}

Summary: Java if Statement

  • The if statement is a fundamental control in Java
  • An if statement contains a condition and a code block that will only be executed if the condition returns true
  • A ladder of conditions can be created by combining if, else if, and else statements
  • In a ladder of conditions, only the block of code inside the first condition that returns true will be run, and no other

if Statement Syntax

This is the syntax of the if statement in Java. Don't forget that you can add and remove the else if and else statements as you wish, and don't forget to replace all variables starting with your_.

if (your_condition1) {  
  your_codeblock1
} else if (your_condition2){  
  your_codeblock2
} else {  
  your_codeblock3
}