Decision Making in Java (Syntax & Example)- A Complete Guide for You!

A decision making allows you to make a decision, based upon the condition. The same concept applied in Java. Decision making in java (statements) helps us to make a decision by considering conditions. Java offers different kinds of statement to get the desired results. Let’s discuss them one by one with their syntax and examples.

Before we start our discussion, you should revise the concept of Data Types in Java

Stay updated with the latest technology trends while you're on the move - Join DataFlair's Telegram Channel

Types of Decision-Making in Java

Types of Decision Making in Java

There are 6 types of Decision-making statement in Java-

1. Java If Statement

If statement in java is the simplest selection statement, it checks whether the condition is true or false and if the condition is true then the if statement is executed.

Syntax

if(condition)
{
// Statements to execute if condition is true
}

If Statement in Java

Example of if Statement in java

package com.dataflair.decisionmaking;

public class IfStatementDemo {
  public static void main(String args[])
  {
    int number = 10;
    //condition of If statement
    if (number < 15)
    {
      System.out.println("10 is less than 15");
    }
    // This statement will be executed  as if considers one statement by default
    System.out.println("I am Not in if");
  }
}

Output-if-statement-in-java

For better understanding, you should know the Variables in Java

2. If else Statement in Java

Java if else statement is an upgraded if statement, it tests the condition and if the condition is false then else statement is executed. These statements are popular for decision making in Java.

Syntax

if (condition)
  {
   // Executes this block if condition is true
  }
else
  {
    // Executes this block if condition is false
  }

If Else Statement in Java

Example of if else Statements in java

// Java program to illustrate if-else statement
package com.dataflair.decisionmaking;

public class IfElseDemo {

  public static void main(String args[])
  {
    int number = 25;
    if (number < 15)
      System.out.println("Number is smaller than 15");
    else
      System.out.println("Number is greater than 15");
  }
}

Output-if-else-demo-in-java

3. Nested-If Statements in Java

Java nested if statement is an if or an if else statement within an if statement. It means that we can execute multiple if statements with this. Java allows nesting within the loops.

It’s time to explore more about loops in Java

Syntax

if (condition1)
{
// Executes when condition1 is true
if (condition2)
{
// Executes when condition2 is true
}
}

nested-if

Example of Nested-If statements in java 

// Java program to illustrate nested-if statement
package com.dataflair.decisionmaking;

public class NestedIfDemo {

  public static void main(String args[])
  {
    int number = 10;
    if (number == 10)
    {
      // First if statement
      if (number < 15)
        System.out.println("number is smaller than 15");
      // Nested - if statement Will only be executed when if statement above is true
      if (number < 12)
        System.out.println("number is smaller than 12 too");
      else
        System.out.println("number is greater than 15");
    }
  }
}

Output-Nested-if-demo

4. If elseIf Ladder Statement in Java

In Java if elseif ladder, the user can use many if else statement within the loop. If one condition holds true the rest of the loops is bypassed.

Recommended Reading – For loop in Java

Syntax 

if (condition)
statement;
else if (condition)
statement;
.
.
else
statement;

 If elseIf Ladder Statement in Java

Example

// Java program to illustrate if-else-if ladder
package com.dataflair.decisionmaking;

public class IfElseIfDemo {

  public static void main(String args[])
  {
    int number = 20;
    if (number == 10)
      System.out.println("number is 10");
    else if (number == 15)
      System.out.println("number is 15");
    else if (number == 20)
      System.out.println("number is 20");
    else
      System.out.println("number is not present");
  }
}

Output-if-else-if-demo

5. Switch Case in Java

The switch statement in java provides an easy way to execute conditions to different parts of the code. Here, a default statement can also be introduced, there must be no duplicate cases too. It executes only one statement from multiple conditions

Syntax

switch (expression)
{
case value1:
statement1;
break;
case value2:
statement2;
break;
.
.
case valueN:
statementN;
break;
default:
statementDefault;
}

switch-case-in-java

Example of Switch Case statements in java

// Java program to illustrate switch-case
package com.dataflair.decisionmaking;

public class SwitchCaseDemo {
  public static void main(String args[])
  {
    int expression = 9;
    switch (expression)
    {
    case 0:
      System.out.println("Expression is zero.");
      break;
    case 1:
      System.out.println("Expression is one.");
      break;
    case 2:
      System.out.println("Expression is two.");
      break;
      //if the expression doesn't match any case then the default statement will be executed
    default:
      System.out.println("Expression is greater than 2.");
    }
  }
}

Output-switch-case-demo

6.  Jump statements in Java

In Java decision making, there are three types of Jump Statements, let’s discuss them in detail:

  • Break
  • Continue
  • Return

These statements are responsible for transferring control from one part to another part of the program. Before we move towards jump statements, let’s revise Operators in Java

6.1 Break Statement in Java

Java break statement used to terminate the program, or it is also used as a form of goto statement.

  • When used for an exit

break-statement

Example of exit statements in java

// Java program to illustrate using break to exit a loop
package com.dataflair.decisionmaking;

public class BreakStatementDemo {

  public static void main(String args[])
  {
    // Initially loop is set to run from 0-9
    for (int iterator = 0; iterator < 10; iterator++)
    {
      // terminate loop when iterator is 5.
      if (iterator == 5)
        break;
      System.out.println("iterator: " + iterator);
    }
    System.out.println("Loop complete.");
  }
}

Output-break-satement-demo

  •  When used as goto statement

Syntax 

label:
{
statement1;
statement2;
statement3;
.
.
}

Example of Goto statements in java

// Java program to illustrate using break with goto
class BreakLabelDemo
  {
  public static void main(String args[])
    {
    boolean booleanVariable = true;
    // label first
    first:
      {
      // Illegal statement here as label second is not introduced yet break second;
      second:
        {
        third:
          {
          // Before break
          System.out.println("Before the break statement");
          // break will take the control out of second label
          if (booleanVariable)
          break second;
          System.out.println("This won't execute.");
          }
          System.out.println("This won't execute.");
        }
        // Third block
        System.out.println("This is after second block.");
      }
    }
  }

Output-Go-to-demo

6.2 Continue Statement in Java

This decision-making statement in Java is used to continue an iteration of a loop, i.e. to continue for a loop to run with a different condition.

Continue Statement in Java

Example of continue statements in java

// Java program to illustrate using continue in an if statement
package com.dataflair.decisionmaking;

public class ContinueDemo {
  public static void main(String args[])
      {
      for (int iterator = 0; iterator < 10; iterator++)
        {
        // If the number is even skip and continue
        if (iterator%2 == 0)
        continue;
        // If number is odd, print it
        System.out.print(iterator + " ");
        }
      }
    }

Output-continue-statement

6.3 Java Return Statement

A return statement in Java is used to return the control to a method.

Do you know what are methods and how to declare them?

Example of return statements in java

// Java program to illustrate using return
class ReturnDemo
  {
  public static void main(String args[])
    {
    boolean booleanVariable = true;
    System.out.println("Before the return.");
    if (booleanVariable)
    return;
    // Compiler will bypass every statement after return
    System.out.println("This won't execute.");
    }
  }

Output-return-statement

Summary

All kind of decision making Statement in Java is designed to ease the coding for the user. This statement automatically checks the condition and execute (if true)  or terminate (if false). Hope, you will also use these statements in your program for faster results.

If you face any difficulty in this tutorial, you can share with us!

It’s the right time to explore a new concept, Polymorphism in Java with real-time examples

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.