Java if Statement: Syntax and Usage with Examples

Rumman Ansari   Software Engineer   2024-07-04 03:27:48   7595  Share
Subject Syllabus DetailsSubject Details 3 Program
☰ TContent
☰Fullscreen

Table of Content:

if statement examples in java

Example 1: if statement

class IfExample {
    public static void main(String[] args){
		int number =2 ;
		if(number > 0) {
		System.out.println("number is positive");
	 }
   }
}

Output

number is positive
Press any key to continue . . .

Example 2: if statement

class IfExample {
    public static void main(String[] args){
		int number = -2 ;
		if(number < 0) {
		System.out.println("number is Negative");
	 }
   }
}

Output

number is Negative
Press any key to continue . . .

Example 3: if statement

public class IfExample {
public static void main(String[] args) {
    int age=21;
    if(age>20){
        System.out.println("Age is greater than 20");
    }
  }
 }

Output

Age is greater than 20
Press any key to continue . . .

Example 4: if statement

public class IfExample {
public static void main(String[] args) {
    int age=15;
    if(age>20){
        System.out.println("Age is greater than 20");
    }
  }
 }

Output

Press any key to continue . . .

Example 5: if statement

import java.util.Scanner;

public class IfExample {
 public static void main(String[] args) {
     Scanner input = new Scanner(System.in);
     System.out.println("Enter an integer: ");
     int number = input.nextInt();

     if (number % 5 == 0)
        System.out.println("Divisiable Five");

     if (number % 2 == 0)
       System.out.println("This is Even Number");
  }
}

Output 1

Enter an integer:
10
Divisiable Five
This is Even Number
Press any key to continue . . .

Output 2

Enter an integer:
8
This is Even Number
Press any key to continue . . .

Output 3

Enter an integer:
13
Press any key to continue . . .

Example 6: if statement

public class IfExample {
public static void main(String[] args) {
     int marks = 85;
	        String grade="fail";

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

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

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

Output

Grade = Pass
Press any key to continue . . .

Example 7: We can have multiple conditions inside If

  • We can use, &&|| operators in order to refine condition.
  • && operator will check whether both left hand AND right-hand conditions are true or not.
  • || operator will check whether both left hand OR right-hand conditions are true or not. If First Condition is true then || operator will skip checking second condition. 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 not execute if block.

Example as: || operator

public class IfExample {
public static void main(String[] args) {
     int marks = 85;
      if(marks > 70 || marks < 90)
        System.out.println("Class A");
  }
}

Output

Class A
Press any key to continue . . .

Example as: && operator

public class IfExample {
public static void main(String[] args) {
     int marks = 85;
      if(marks > 70 && marks < 90)
        System.out.println("Class A");
  }
}

Output

Class A
Press any key to continue . . .

Explanation about some important points inside if statement

If First Condition is true then || operator will skip checking second condition.

public class IfExample {
public static void main(String[] args) {
    int number1 = 5;
	  int number2 = 10;

	  if ((++number1 > 0) || (++number2 > 0))
	     {
	     System.out.println(number1);
	     System.out.println(number2);
	     }

  }
}

Output

6
10
Press any key to continue . . .

Explanation

In the above example there is two condition, in left side (++number1 > 0) and in right side (++number2 > 0) First condition (++number1 > 0) is true so it skip out checking second condition (++number2 > 0) . For that it increase the value of number1 which gives 6. Second condition is not checked for that the output is still 10.

If First Condition is false then || operator will check second condition if second condition is true then overall if will follow true block

public class IfExample {
public static void main(String[] args) {
    int number1 = 5;
	  int number2 = 10;

	  if  ((++number1 < 0) || (++number2 > 0))
	     {
	     System.out.println(number1);
	     System.out.println(number2);
	     }

  }
}

Output

6
11
Press any key to continue . . .

Explanation

In the above example there is two condition one is left side (++number1 < 0) and in right side (++number2 > 0) First condition (++number1 < 0) is false because number1 is grater than 0 and it increment the value of number1 which gives 6 , as it is a false condition it goes to condition 2 (++number2 > 0) . And it increase the value of number2 which gives 11.

If First Condition is true then && operator will check second condition.

public class IfExample {
public static void main(String[] args) {
    int number1 = 5;
	  int number2 = 10;

	  if  ((++number1 > 0) && (++number2 > 0))
	     {
	     System.out.println(number1);
	     System.out.println(number2);
	     }

  }
}

Output

6
11
Press any key to continue . . .

Explanation

In the above example there is two condition, in left side (++number1 > 0) and in right side (++number2 > 0) First condition (++number1 > 0) is true because number1 is grater than 0 and it incrase the value of number1 which gives 6 , as it is a true condition it goes to condition 2 (++number2 > 0) . And it increase the value of number2 which gives 11.

If First Condition is false then && operator will skip checking second condition.

public class IfExample {
public static void main(String[] args) {
    int number1 = 5;
	  int number2 = 10;

	  if  ((++number1 < 0) && (++number2 > 0))
	     {
	     System.out.println(number1);
	     System.out.println(number2);
	     }

  }
}

Output

Press any key to continue . . .

Explanation

In the above example there is two condition, in left side (++number1 < 0) and in right side (++number2 > 0) First condition (++number1 < 0) is false because number1 is grater than 0 and it skip checking second condition (++number2 > 0) . So over all if block is false for that it will not execute if block.