LogIn
I don't have account.

if else in java

DevSniper
113 Views

if is a control flow statement of programing language. if statement is used to control execution of a code block based on certain condition. if provided condition evaluates to true then program execution enter inside if block of code. In Java , there are four types of if-statements.

  • simple if statement
  • if-else statement
  • if-else-if statement
  • nested if statement

Simple if statement

if is used to run a code block based on certain condition. i.e. you can use if statement when you want to execute some block of code based on certain condition. if condition is true code block will be executed.
if(condition)
{
 //code block
}
public class RebootMemory
{
    public static void main(String []args)
    {
        int number=4;
        if(number%2==0)
        {
            System.out.println(number + " is an Even Number");
        }
    }
}
4 is an Even Number

if-else statement

if you have a case where you want to execute two code block conditionally. i.e. any one code block will execute based on condition means if condition is true 1st code block otherwise 2nd code block will execute.

if(condition)
{
  // 1st code block
}
else
{
 // 2nd code block
}
public class RebootMemory
{
    public static void main(String []args)
    {
        int number=5;
        if(number%2==0)
        {
            System.out.println(number + " is an Even Number");
        }
        else
        {
            System.out.println(number + " is an Odd Number");
        }
    }
}
5 is an Odd Number

if-else-if statement

if you have a case where you want to execute code blocks based on more than one condition you can use if-else-if statement. you can define as many as conditions as per your program requirement.

if(condition1)
{
  // 1st code block
}
else if (condition2)
{
 // 2nd code block
}
else if(condition3)
{
 // 3rd code block
}
.............................
..................
else
{
 //default code block
}

Example :- Grading System

public class RebootMemory 
{
    public static void main(String[] args) 
    {
        int percentage = 75;
        if (percentage >= 90) {
            System.out.println("A Grade");
        } else if (percentage >= 80) {
            System.out.println("B Grade");
        } else if (percentage >= 70) {
            System.out.println("C Crade");
        } else if (percentage >= 60) {
            System.out.println("D Drade");
        } else {
            System.out.println("F Frade");
        }
    }
}
C Crade

Example 2 :- Leap Year

public class RebootMemory 
{
    public static void main(String[] args) 
    {
        int year = 2024;
        if (year % 400 == 0) {
            System.out.println(year + " is a Leap Year.");
        } else if (year % 100 == 0) {
            System.out.println(year + " is not a Leap Year.");
        } else if (year % 4 == 0) {
            System.out.println(year + " is a Leap Year.");
        } else {
            System.out.println(year + " is not a Leap Year.");
        }
    }
}
2024 is a Leap Year.

Nested if statement

Placing one if statement inside another if statement is known as nested if statements . This approach is useful when you need to verify more conditions based on the outcome of an initial condition.

if(condition)
{
  ...............................
  // nested if statement
   if(nestedCondition)
   {
     // .......................
   }
  .........................................
}
public class RebootMemory 
{
    public static void method(int percentage) 
    {
        if(percentage >=0 &amp;&amp; percentage <=100) {
            if (percentage >= 90) {
                System.out.println("A Grade");
            } else if (percentage >= 80) {
                System.out.println("B Grade");
            } else if (percentage >= 70) {
                System.out.println("C Crade");
            } else if (percentage >= 60) {
                System.out.println("D Drade");
            } else {
                System.out.println("F Frade");
            }
        } else {
            System.out.println("Invalid percentage. Please enter percentage between 0 and 100.");
        }
    }
    public static void main(String[] args) {
        System.out.print("75 percentage => ");
        method(75);
        System.out.print("-65 percentage => ");
        method(-65);
        System.out.print("101 percentage => ");
        method(101);
    }
}
75 percentage => C Crade
-65 percentage => Invalid percentage. Please enter percentage between 0 and 100.
101 percentage => Invalid percentage. Please enter percentage between 0 and 100.