Java If Statement

In this chapter you will learn:

  1. Learn the simplest form of if statement
  2. How to the if/else statement
  3. How to use the nested if statements
  4. Learn how to use the if-else-if Ladder

Basic If Statement

The following the simplest form of Java if statement:

if(condition) 
   statement;

condition is a boolean expression. If condition is true, then the statement is executed. If condition is false, then the statement is bypassed.

Here is an example:

public class Main {
/*j  a va 2 s .  co  m*/
  public static void main(String args[]) {
    int num = 99;
    if (num < 100) {
      System.out.println("num is less than 100");

    }
  }
}

The output generated by this program is shown here:

If statement is often used to to compare two variables. The following code defines two variables, x and y, the it uses the if statement to compare them and prints out messages.

public class Main {
//  j  a va2  s. c o m
  public static void main(String args[]) {
    int x, y;

    x = 10;
    y = 20;

    if (x < y){
      System.out.println("x is less than y");
    }

    x = x * 2;
    if (x == y){
      System.out.println("x now equal to y");
    }

    x = x * 2;
    if (x > y){
      System.out.println("x now greater than y");
    }

    if (x == y){
      System.out.println("===");
    }
  }
}

The output generated by this program is shown here:

We can also use a boolean value to control the if statement. The value of a boolean variable is sufficient, by itself, to control the if statement.

public class Main {
  public static void main(String args[]) {
    boolean b;/*from   jav a  2 s .  c o  m*/
    b = false;
    if (b) {
      System.out.println("This is executed.");
    } else {
      System.out.println("This is NOT executed.");
    }

  }
}

There is no need to write an if statement like this:

if(b == true) ...

The output generated by this program is shown here:

If else statement

The if statement is a conditional branch statement. Here is the general form of the if-else statement:

if (condition) 
   statement1; 
else 
   statement2;

The else clause is optional. Each statement may be a single statement or a compound statement enclosed in curly braces (a block). Only one statement can appear directly after the if or the else. To include more statements, you'll need to create a block, as in this fragment:

public class Main {
  public static void main(String[] argv) {
    int i = 1;//j  av a  2  s.co  m

    if (i > 0) {
      System.out.println("Here");
      i -= 1;

    } else
      System.out.println("There");
  }
}
  ]]>

The output:

It is clear to include the curly braces when using the if statement, even when there is only one statement in each clause.

Nested if statements

A nested if is an if statement inside another another if statement or else. The following code uses a nested if statement to compare values.

public class Main {
  public static void main(String[] argv) {
    int i = 10;/*from   j a  va 2  s  .c om*/
    int j = 4;
    int k = 200;
    int a = 3;
    int b = 5;
    int c = 0;
    int d =0;
    if (i == 10) {
      if (j < 20){
        a = b;
      }
      if (k > 100){
        c = d;
      }
      else{
        a = c;
      }
    } else{
      a = d;
    }
    System.out.println("a = " + a);
    System.out.println("b = " + b);
    System.out.println("c = " + c);
    System.out.println("d = " + d);
    
  }
}

The output:

The if-else-if Ladder

The if-else-if Ladder looks like this:

if(condition) /*java2  s. com*/
   statement; 
else if(condition) 
   statement; 
else if(condition) 
   statement; 
. 
.
else 
   statement;

Here is a program that uses an if-else-if ladder.

public class Main {
  public static void main(String args[]) {
    int month = 4;
    String value;/*  jav a2s.  c  o m*/
    if (month == 1 )
      value = "A";
    else if (month == 2)
      value = "B";
    else if (month == 3)
      value = "C";
    else if (month == 4)
      value = "D";
    else
      value = "Error";

    System.out.println("value = " + value);
  }
}

Here is the output produced by the program:

Next chapter...

What you will learn in the next chapter:

  1. What is the Java Switch Statement Syntax
  2. How to use the optional break statement
  3. How to write nested switch Statements
Home » Java Tutorial » Operators Statements
Your first Java program
Your second Java program
Java Keywords and Identifiers
Variable stores value in a Java program
Variable Scope and Lifetime
Java Operators
Java Arithmetic Operators
Java Bitwise Operators
Java Relational Operators
Java Boolean Logical Operators
Java If Statement
Java switch Statement
Java while Loop
Java for loop
Java for each loop
Java break statement
Java continue statement
Java return statement
Java Comments
Java documentation comment(Javadoc)