If else in java

Let’s learn if else in java.

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 java code; statements inside “if” executes if condition is true else statements inside “else” will execute. Here’s the syntax for if else statement,

Syntax:

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


if else in java

if else statement java example:

In the below program 15 modulo 2 is not equal to 0, hence it is odd number. So the control shifts to else condition and prints 15 is odd number.

If 15 modulo 2 was equal to 0 then output would be even number.

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

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

Also read – switch statement in java

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 ladder

else if ladder checks multiple conditions which are dependent on each other. Also in else if ladder only one condition can be true.

Also read – iterate over array using foreach loop in java

Syntax:

if(condition1) 
{ 
   // execute if condition1 is true 
} 
else if (condition2) 
{ 
   // execute if condition2 is true 
}
else if (condition3)
{
   // execute if condition3 is true
} 
else 
{ 
   // execute if conditions 1, 2 and 3 becomes false 
}


Let’s see an example on else if ladder in java,

import java.util.Scanner;

public class ElseIfLadder
{
   public static void main(String[] args)
   {
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter students percentage: ");
      float percentage = sc.nextFloat();
      if(percentage >= 70)
      {
         System.out.println("student has got distinction.");
      }
      else if(percentage >= 60)
      {
         System.out.println("student has got first class.");
      }
      else if(percentage >= 50)
      {
         System.out.println("student has got second class.");
      }
      else if(percentage >= 40)
      {
         System.out.println("student has just pass.");
      }
      else
      {
         System.out.println("student is failed.");
      }
      sc.close();
   }
}


Output :

Please enter students percentage: 90
student has got distinction.


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 Demo
{
   public static void main(String[] args)
   {
      int num = 50;
      if(num > 41)
      {
         System.out.println("50 is greater than 41");
      }      
   }
}



Output :

Employee age is greater than 41!!


Ternary operator – if else shorthand

Ternary operator is used as shorthand for if else. Ternary operator has three operands.

Syntax:

variable = (condition) ? True : False;

Example:

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

We can also write above code like this,

int number = 100;
if(number < 50)
{
   System.out.println("Less than 50");
}
else
{
   System.out.println("Greater than 50");
}



if else statement exercises

Now let’s see conditional statement exercises. Here we are going to execute if else statement exercise or java program which allow user to input age using Scanner class.

Then using conditional statement, i.e, if else statement will show if user is eligible for driving licence. So a user should be older than or equal to 18 to get driving licence.

import java.util.Scanner;

public class Exercise 
{
   public static void main(String[] args) 
   {
      int age;
      Scanner sc = new Scanner(System.in);
      System.out.print("Input your age: ");
      age = sc.nextInt();
      if(age >= 18)
      {
         System.out.println("You are eligible for driving licence.");
      }
      else
      {
         System.out.println("You are not eligible for driving licence.");
      }
      sc.close();
   }
}



Output:

Input your age: 24
You are eligible for driving licence.

Input your age: 15
You are not eligible for driving licence.


Now let’s see another if else statement exercise or java program where we are going to check whether given number is even number or odd number. Let’s see if else statement java exercises,

import java.util.Scanner;

public class Exercise 
{
   public static void main(String[] args) 
   {
      Scanner sc=new Scanner(System.in);
      System.out.print("Please enter number: ");
      int number = sc.nextInt();
      if(number%2 == 0)
      {
         System.out.println("even number.");
      }
      else
      {
         System.out.println("odd number.");
      }
      sc.close();
   }
}


Output:

Please enter number: 12
even number.

Please enter number: 15
odd number.