Java If Statement



If Statement in Java Programming : Conditional Selection

  1. The if statement is a conditional branch statement.
  2. It tells your program to execute a certain section of code only if a particular test evaluates to true.
  3. If boolean Expression evaluates to true, the statements in the block following the if statement are executed.
  4. If it evaluates to false, the statements in the if block are not executed.

if statement in java programming language

Syntax : If Statement

if (booleanExpression) {
    statement(s)
}
  1. If boolean Expression evaluates to false and there is an else block, the statements in the else block are executed.

Syntax : If-Else Statement

if (booleanExpression) {
    statement(s)
} else {
    statement(s)
}

Syntax : If-Else Ladder Statement

  1. If we have multiple conditions then we can use if-else ladder –
if (booleanExpression1) {
    // statements
} else if (booleanExpression2) {
    // statements
}
...
else {
     // statements
}

Live Example 1 : Simple If-Else Ladder Statement

class IfDemo {
    public static void main(String[] args) {

        int marks = 76;
        String grade;

        if (marks >= 40) {
            grade = "Pass";
        }

        if (marks < 40) {
            grade = "Fail";
        }

        System.out.println("Grade = " + grade);
    }
}

Output :

Grade = Pass

Live Example 2 : Simple If-Else Ladder Statement

class IfDemo {
    public static void main(String[] args) {

        int marks = 76;
        String grade;

        if (marks >= 40) {
            grade = "Pass";
        }
        else
        {
            grade = "Fail";
        }

        System.out.println("Grade = " + grade);
    }
}

Output :

Grade = Pass

Live Example 3 : Simple If-Else Ladder Statement

class IfElseDemo {
    public static void main(String[] args) {

        int marks = 76;
        char grade;

        if (marks >= 90) {
            grade = 'A';
        } else if (marks >= 80) {
            grade = 'B';
        } else if (marks >= 70) {
            grade = 'C';
        } else if (marks >= 60) {
            grade = 'D';
        } else {
            grade = 'F';
        }
        System.out.println("Grade = " + grade);
    }
}

Output :

Grade = C

Live Example 4 : We can have multiple conditions inside If

if(marks > 70 || marks < 90)
        System.out.println("Class A");
else
        System.out.println("Class B");
  1. We can use “&&” , “||” operators in order to refine condition.
  2. && operator will check whether both left hand and right hand conditions are true or not.
  3. || condition will check

Must Read Note :

If we have written two conditions inside “If Condition” then –

  1. If First Condition is true then || operator will skip checking second condition.
  2. If First Condition is false then || operator will check second condition if second condition is true then overall if will follow true block otherwise it will follow else block.

Proof –

class Test
{
 public static void main(String args[])
  {
  int num1 = 10;
  int num2 = 20;

  if  ((++num1 > 0) || (++num2 > 0))
     {
     System.out.println(num1);
     System.out.println(num2);
     }

  }
}

Output :

11
20

Explanation :

  1. First condition is true so it skip out checking second condition.
class Test
{
 public static void main(String args[])
  {
  int num1 = 10;
  int num2 = 20;

  if  ((++num1 < 0) && (++num2 > 0))
     {
     System.out.println(num1);
     System.out.println(num2);
     }

      System.out.println(num1);
     System.out.println(num2);

  }
}

Output :

11
20

Explanation :

  1. If First Condition is true then && operator will check second condition.
  2. If First Condition is false then && operator will skip checking second condition.