If else in java

Let’s learn if else in java.

In java, decision making is made through control flow statements based on a condition. Condition is based on boolean condition: true or false.

If else statement

executes a block of code if condition is false.

Also read – do while loop java

Syntax:

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


if else in java

Example:

import java.io.*;

public class JavaIfElse
{
   public static void main(String[] args)
   {
      int number = 15; 
      // check if number is divisible by 2 
      if(number%2 == 0)
      { 
         System.out.println(number + " is even number"); 
      }
      else
      { 
         System.out.println(number + " is odd number"); 
      }
   }
}



Output:

15 is even number


Nested if else statement

Nested if else statement means one under the other. That is you can write one if else statement inside another if else statement.

Also read – java for loop example

public class Demo
{
   public static void main(String[] args)
   {
      Integer num1 = 2, num2 = 8, num3 = 10, largest;
      if(num1 >= num2) 
      {
         if(num1 >= num3) 
         {
            largest = num1;
         } 
         else
         {
            largest = num3;
         }
      } 
      else
      {
         if(num2 >= num3) 
         {
            largest = num2;
         } 
         else
         {
            largest = num3;
         }
      }
      System.out.println("Largest number " + largest);
   }
}



Output:

Largest number 10


else if… statement

else-if statement is used when we need to check multiple conditions. Here use else-if statement if first condition is false.

Also read – iterate over array using foreach loop in java

It’s also known as “If-else-if-else ladder” statement.

Syntax:

if(condition1) 
{ 
   // if condition1 is true 
} 
else if (condition2) 
{ 
   // if condition1 is false and condition2 is true 
} 
else 
{ 
   // if condition1 is false and condition2 is false 
}



Example:
public class Example
{
   public static void main(String[] args)
   {
      int num = 0;
      if(num > 0) 
      {
         System.out.println("Number is +ve.");
      }
      else if(num < 0) 
      {
         System.out.println("Number is -ve.");
      }
      else
      {
         System.out.println("Number is 0.");
      }
   }
}



Output :

Number is 0.


If statement

“If” statement executes a set of statements if condition/expression is true.

Syntax:

if(condition)
{
   // if condition is true;
}

if else in java

Example:

public class IfStatement
{
   public static void main(String[] args)
   {
      int empAge = 50;
      if(empAge > 41)
      {
         System.out.println("Employee age is greater than 41!!");
      }      
   }
}



Output :

Employee age is greater than 41!!


Ternary operator

Ternary operator is used as a shorthand for if… else statement.

Syntax:

variable = (condition) ? True : False;

Example:

int number = 100;
String result = (number < 50) ? “Less than 50” : “Greater than 50”;
System.out.println(result);