Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

Java If Else Program

In this program, we will perform various programs using the if-else statement in java. But before moving forward, if you are not familiar with the concept of if statement in java, then do check the article on the topic Conditional Statement in Java.

Syntax

if(condition){  
//code if condition is true  
}else{  
//code if condition is false  
}  

In this tutorial, we will see the programs of three types of if-statements:

  1. if-statement

  2. if-else statement

  3. if-else-if ladder

Let us look at each of these programs separately.

Program 1: If Program in Java

In this program, we will see the implementation of the if statement in java. Here, we will determine whether a person is eligible to vote or not based on his/her age.

Algorithm:

  1. Start.

  2. Create an instance of the Scanner class.

  3. Declare a variable to store the age of the person.

  4. Ask the user to initialize the variable.

  5. Use an if statement to check if the person is eligible to vote or not.

  6. If the entered age is greater than and equal to 18, then he is eligible to vote.

  7. Display the result.

  8. Stop.

Below is the Java code for if conditional program.

//Java Program for implementation of if statement
import java.util.Scanner;
public class Main
{
     public static void main(String []args)
     {
         //Take input from the user
         //Create an instance of the Scanner class
         Scanner sc=new Scanner(System.in);
         System.out.println("Enter the age: ");
         int age=sc.nextInt();
         //Determine whether the person is eligible to vote or not
          if(age>=18)
          {  
                System.out.println("The person is eligible to vote");  
          }     
     }
}


Enter the age: 21
The person is eligible to vote

Program 2: Java If-Else Program

In this program, we will see the implementation of the if-else statement in java. Here, we will determine whether the entered number is positive or negative using an if-else statement.

Algorithm:

  1. Start

  2. Create an instance of the Scanner class.

  3. Declare a variable to store the number.

  4. Ask the user to initialize the number.

  5. Use an if-else statement to check if the entered number is positive or negative.

  6. If the entered number is greater than 0, then print it as a positive number.

  7. Else if the entered number is less than 0, then print it as a negative number.

  8. Display the result.

  9. Stop.

Below is the Java code for if-else conditional program.

//Java Program for implementation of if-else statement
import java.util.Scanner;
public class Main
{
     public static void main(String []args)
     {
         //Take input from the user
         //Create an instance of the Scanner class
         Scanner sc=new Scanner(System.in);
         System.out.println("Enter a number: ");
         int num=sc.nextInt();
         if(num>0)
         {
             System.out.println("The entered number "+num+" is positive.");
         }
         else
         {
             System.out.println("The entered number "+num+" is negative.");
         }
     }
}


Enter a number: -9
The entered number -9 is negative.

Program 3: Java If-else-if Program

In this program, we will see the implementation of the if-else-if ladder in java. Here, we will determine the grades of a student from the marks obtained by him.

Algorithm:

  1. Start

  2. Create an instance of the Scanner class.

  3. Declare a variable to store the marks obtained.

  4. Ask the user to initialize the variable.

  5. Use an if-else-if statement to determine the grades obtained from the entered marks.

  6. If marks are less than 50, print as fail.

  7. Else if marks lie between 50 to 60, print as a D grade.

  8. Else if marks lie between 60 to 70, print as a C grade.

  9. Else if marks lie between 70 to 80, print as B grade.

  10. Else if marks lie between 80 to 90, print as an A grade.

  11. Else if marks lie between 90 to 100, print as an A+ grade.

  12. Else print as an invalid.

  13. Display the result.

  14. Stop.

Below is the Java code for the if-else conditional program.

//Java Program for implementation of if-else-if statement
import java.util.Scanner;
public class Main
{
     public static void main(String []args)
     {
         //Take input from the user
         //Create an instance of the Scanner class
         Scanner sc=new Scanner(System.in);
         System.out.println("Enter the marks obtained: ");
         int num=sc.nextInt();
         //Determine the grades from the marks obtained
          if(num<50)
          {  
                System.out.println("fail");  
          }  
          else if(num>=50 && num<60)
          {  
                System.out.println("D grade");  
          }  
          else if(num>=60 && num<70)
          {  
               System.out.println("C grade");  
          }  
          else if(num>=70 && num<80)
          {  
               System.out.println("B grade");  
          }  
          else if(num>=80 && num<90)
          {  
              System.out.println("A grade");  
          }
          else if(num>=90 && num<100)
          {  
              System.out.println("A+ grade");  
          }
          else
          {  
             System.out.println("Invalid!");  
          }  
     }
}


Enter the marks obtained: 78
B grade