Table of contents
1.
Introduction
2.
The if statement
2.1.
Nested if statements
3.
The if-else statement
3.1.
Short-hand notation
4.
The else-if ladder
5.
FAQs
6.
Key Takeaways
Last Updated: Mar 27, 2024

Decision Statements in Java

Author Yashesvinee V
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Java executes all its programs in sequential order. Often, the flow of execution may depend on certain conditions that the programmer may want to impose. Decisional control statements require a condition to be fulfilled to execute the set of instructions that follow it. Relational and logical operators play an important role in setting such conditions. Relational operators are used to compare two operands and determine their relation.

Recommended Topic-  Iteration Statements in Java, and Duck Number in Java.

The if statement

The if control statement allows the execution of a block of code only if it satisfies a given condition. If it does not fulfil the condition, the entire block is skipped. The use of if statement is the simplest way to modify the control flow of a program.

Syntax:

if(condition)
{
  Statements of if block
}
You can also try this code with Online Java Compiler
Run Code

Note: If there are no brackets to include the statements of the if block, only the first line after the if statement is executed as part of it.

Here is an example of the if statement in Java. Try it on online java compiler.

public class Main 
{
   public static void main(String args[])
  {
      int i = 10;
      if (i < 15)
      {
        int difference = 15-i;
        System.out.println("The value is less than 15 by " + difference);
      }
      if (i == 15)
        System.out.println("The value is = " + i);


      System.out.println("End");
  }
}
You can also try this code with Online Java Compiler
Run Code

 

Output:

The value is less than 15 by 5
End

Also read - Jump statement in java

Nested if statements

They have an if statement inside another if statement. The inner block statements are executed only if both condition1 and condition2 are satisfied.

Syntax:

if(condition1)
{
  Outer block Statements
   if (condition2)
   {
     Inner block Statements
   }
}
You can also try this code with Online Java Compiler
Run Code

An example of nested if statements.

public class Main 
{
   public static void main(String args[])
  {
      int i = 10;
      if (i < 15)
      {
        if (i%2 == 0)
            System.out.println("The value is an even number less than 15" +);
      }
      System.out.println("End");
  }
}
You can also try this code with Online Java Compiler
Run Code

 

Output:

The value is an even number less than 15
End

 

You can also read about Java Destructor and Hashcode Method in Java.

The if-else statement

The if-else control statement allows the execution of instructions in the if block only if it satisfies a given condition. If it does not fulfil the condition, then the code present in the else block is executed. The use of this control statement ensures the execution of either the if block or the else block compulsorily.

Syntax:

if(condition)
{
  Statements of if block
}
else
{
   Statements of else block
}
You can also try this code with Online Java Compiler
Run Code

Note: If there are no brackets to include the statements of a block, only the first line after the if/else control statement is executed as part of it.

Here is an example of the if-else statement in Java.

public class Main 
{
   public static void main(String args[])
  {
      int i = 20;
      if (i < 15)
      {
        int difference = 15-i;
        System.out.println("The value is less than 15 by " + difference);
      }
      else
      {
        int difference = i-15;
        System.out.println("The value is greater than 15 by " + difference);
      }
      System.out.println("End");
  }
}
You can also try this code with Online Java Compiler
Run Code

 

Output:

The value is greater than 15 by 5
End

Short-hand notation

The ternary operator ?: can be used as a short-hand of the if-else control statement.

Syntax:

(condition)?result1:result2;
You can also try this code with Online Java Compiler
Run Code

 

If the condition is true, then result1 is the answer. Otherwise, result2 is the answer.

public class Main 
{
  public static void main(String args[])
  {
      int i = 10;
      String Answer = (i%2 == 0)?"Even":"Odd";
      System.out.println(Answer);
  }
}
You can also try this code with Online Java Compiler
Run Code

 

Output:

Even

Also see, Swap Function in Java

The else-if ladder

The else-if ladder allows the execution of the instructions in the if block or the consecutive else-if block only if it satisfies their given condition. The else block is executed if none of the conditions are met. The else-if control statements are used when multiple cases are possible for a given situation, but a single and final output is required.

Syntax:

if(condition1)
{
   Statements
}
else if(condition2)
{
  Statements
}
else if(condition3)
{
  Statements
}
...
else
{
  Statements
}
You can also try this code with Online Java Compiler
Run Code

Here is an example of the else-if ladder in Java.

public class Main 
{
   public static void main(String args[])
  {
      int i = 15;
      if (i < 15)
        System.out.println("The value is less than 15 by " + (15-i));
      else if (i > 15)
        System.out.println("The value is greater than 15 by = " + (i-15));
      else
        System.out.println("The value is equal to 15");
   }
}
You can also try this code with Online Java Compiler
Run Code

 

Output:

The value is equal to 15

 

You can also check out Has-A Relationship in Java here.

Must Read Conditional Statements in Java

Related Article What are Loops in Java

FAQs

  1. What are logical operators?
    && (AND) - It returns true only if both its operands are true.
    || (OR) - It returns true if at least one of its operands is true.
    ! (NOT) - It returns true if its operand is false and vice versa.
     
  2. What is the main difference between the else if ladder and switch-case statements?
    The else if ladder executes a set of statements based on boolean values while a switch case statement executes a block of statements based on the equality of a variable to a case.

Key Takeaways

Decisional control statements have a lot of practical applications and are extremely useful to programmers when they come across situations where a constraint is to be added. This blog has discussed the list of control statements in Java and a few examples to understand them better.

Related Links:

Conditional statements in C++

Conditional Statements in Python

Check out the official Coding Ninjas Blog site and visit our Library for more.

Live masterclass