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.

else statement

executes a block of java code if condition is false.

Syntax:

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


if else in 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 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 – 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


Java else if statement

else if statement defines a new condition. Here use else if statement if first condition is false.

Also read – iterate over array using foreach loop in java

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 = 10;
      if(num < 0) 
      {
         System.out.println("Number is -ve.");
      }
      else if(num < 5) 
      {
         System.out.println("Number is 0.");
      }
      else
      {
         System.out.println("Number is +ve.");
      }
   }
}



Output :

Number is +ve.


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 – short hand if…else

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.