JAVA IF ... ELSE

Java if else is a conditional statement that is used when we want to execute a block of code based on some condition, the condition may evaluate to either true or false. If you dont know what are conditional statements kindly go through Java Operators tutorials.

Table of Contents

[hide]
  • Java if Statement
  • Java if else Statement
  • Java if else if Statement
  • Java if else Shorthand ( Ternary operator )

Java if Statement

Java if is a conditional statement that executes the if block if a specified condition is true.

Syntax :

if (condition){
// executes this block if the condition evaluates to true
}

Java is a case-sensitive language, hence if is in lowercase. Any other form would result in an error.

Example :

copyimage
// Java program to illustrate the use of if statement
import java.util.*;
public class IfStatementExample{
      public static void main(String args[]){  
           Scanner sc= new Scanner(System.in); 
           System.out.print("Enter any amount ");
           int amount= sc.nextInt(); 

           if(amount > 1000 ){ // conditional check 
              System.out.println("Entered amount is greater than 1000");
           } 
     }
}

OUTPUT

Enter any amout : 2000Entered amount is greater than 1000

Explanation

In the above example we are taking the input from user, if you dont have any idea how to take input from the user then kindly have a look at Java User Input tutorial.

In the example we have entered the amount as 2000 that is greater than 1000, hence the condition evaluates to true and the if block will get executed. If you provide any input that is less than 1000, the condition will become false and it will not execute anything.

Java if else Statement

Java if else checks for the condition to be true or false. If block gets executed if the condition is true, and if the condition is false then else block is executed.

Syntax :

if (condition){
// executes this block if the condition evaluates to true
} else {
// executes this block if the condition evaluates to false}

Example 1 :

copyimage
// Java program to illustrate the use of if else statement
import java.util.*;
public class IfStatementExample{
      public static void main(String args[]){  
           Scanner sc= new Scanner(System.in); 
           System.out.print("Enter any amount ");
           int amount= sc.nextInt(); 

           if(amount > 1000 ){ // conditional check 
              System.out.println("Entered amount is greater than 1000");
           } else {
              System.out.println("Entered amount is less than 1000");
           }
     }
}

OUTPUT

Enter any amout : 500Entered amount is less than 1000

Explanation

This example is same as that we have taken in the Java if statement. But here we have input the number 500 which is less than 1000, therefore Java else block gets executed.

Example 2 :

copyimage
// Java program to illustrate the use of if else statement
import java.util.*;
public class IfStatementExample{
      public static void main(String args[]){  
          Scanner sc= new Scanner(System.in); 
          System.out.print("Enter any num ");
          int num= sc.nextInt(); 

          if(num % 2 == 0 ){ // conditional check 
            System.out.println("Entered number is even");
          } else {
            System.out.println("Entered number is odd");
        }
     }
}

OUTPUT

Enter any num : 22Entered number is even
Enter any num : 17Entered number is odd

You can try the given programs in your system and test with various inputs.

Java if else if Statement

Java if else if checks for the condition block by block. If the first condition is false, it will check the next condition and so on. As soon as a condition of a block evaluates to true that particular block gets executed.

Syntax :

if (condition1){
// executes this block if condition1 is true
} else if (condition2) {
// executes this block if condition1 is false and condition2 is true
} else {
// executes this block if all the above conditions are false}

Example :

copyimage
// Java program to illustrate the use of if else if statement
import java.util.*;
public class IfElseIfExample{
     public static void main(String args[]){  
         Scanner sc= new Scanner(System.in); 
         System.out.print("Enter any value ");
         int age= sc.nextInt(); 

         if(age<=13) {// conditional check 
             System.out.println(" You are a Child ");
         } else if (age >=13 && age <18) {
             System.out.println(" You are a Teenager ");
         } else if (age >=18) {
             System.out.println(" You are an Adult ");
         }
    }
}

OUTPUT

Enter any value : 12You are a Child
Enter any value : 17You are a Teenager
Enter any value : 33You are an Adult

Try this example in your system with multiple inputs. Moreover you can write a program to find out Grades according to the marks obtained. Example one condition could be if marks < 40 then print fail.

Java if else Shorthand ( Ternary Operator )

Shorthand if else statements are also known as ternary operators because they consists of three operends. There are often used to replace simple if else statement by reducing the number of lines.

Syntax

variable result = (condition) ? (statement1) :(statement2)

Example :

copyimage
// Java program to illustrate the use of if else Shorthand statement
import java.util.*;
public class ShorthandExample {
     public static void main(String args[]){  
         Scanner sc= new Scanner(System.in); 
         System.out.print("Enter marks obtained ");
         int marks = sc.nextInt(); 

         String result= (marks < 40) ? "Fail" : "Pass"; // Shorthand if else
         System.out.println(" Final result is :"+ result);
    }
}

OUTPUT

Enter marks obtained : 88 Pass
Enter marks obtained : 34 Fail

Hope you have grasp the basics of Java if else through this tutorial. Practice more to get command over any topic.

In next tutorial we will learn about Java Switch Statements .