Decision Making in Java Programming with Syntax and Examples


1. Objective

In this tutorial for Decision Making in Java, we are going to study the various decision-making Statements in Java Programming Languages. If Statement, If-else Statement, Nested-if Statement, If-else-if Statement, Switch Case Statement with their Java Syntax, uses, limitations, and examples.

decision making in java - Introduction

Introduction to Decision Making in java

2. What is Decision Making in Java

Decision-making statements are same as the decision making we face in real life, in programming languages too we face situations similar to real life where we need to apply conditions to take decisions. The most commonly used loops statements in Java are

  1. If
  2. If-else
  3. Nested-if
  4. If-else-if
  5. Switch-case
  6. Jump

Follow this link to know Loops in Java – Types and Examples 

3. Types of Decision-Making in Java

a. Java If Statement

If Statement in java is the simplest selection statement in the Java language, it checks the condition and then the loop is executed.

Syntax

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

If Statement in decision making in java

Decision making in java – If Statement

Example of If Statement in java

class IfDemo
  {
   public static void main(String args[])
     {
           int i = 10;
           if (i > 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");
      }
   }

Now let’s Discuss Java String and Java Datatypes.

b. 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 among Decision Making in Java Statements.

Syntax

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

 If-Else Statement in Decision Making in Java

Decision Making in Java – If-Else StatementExample of If-else Statements in java

// Java program to illustrate if-else statement
class IfElseDemo
  {
    public static void main(String args[])
    {
 		 int i = 10;
     if (i < 15)
     System.out.println("i is smaller than 15");
    else
    System.out.println("i is greater than 15");
    }
  }

c. Nested-If Statements in Java

Java nested if statement is an if statement or an if-else statement within an if statement. Java allows nesting within the loops.

Syntax

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

Nested-If Statements - Decision Making in Java

Decision Making in Java – Nested-If Statements

Example of Nested-If statements in java 

// Java program to illustrate nested-if statement
  class NestedIfDemo
    {
    public static void main(String args[])
      {
        int i = 10;
        if (i == 10)
          {
            // First if statement
            if (i < 15)
            System.out.println("i is smaller than 15");
            // Nested - if statement Will only be executed if statement above it is true
            if (i < 12)
            System.out.println("i is smaller than 12 too");
        else
        System.out.println("i is greater than 15");
          }
      }
  }

To understand programming follow the links Java Class and Object and Variables in Java.

d. If-Else-If Ladder Statement in Java

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

Syntax 

if (condition)
statement;
else if (condition)
statement;
.
.
else
statement;
 If-Else-If Statements in Decision Making In Java

Decision Making In Java – If-Else-If Statements

Example

// Java program to illustrate if-else-if ladder
class ifelseifDemo
  {
    public static void main(String args[])
    {
      int i = 20;
      if (i == 10)
      System.out.println("i is 10");
      else if (i == 15)
      System.out.println("i is 15");
      else if (i == 20)
      System.out.println("i is 20");
    else
    System.out.println("i is not present");
    }
  }

e. Switch Case in Java

The switch statement provides an easy way to execute conditions to different parts of the code. In a switch statement, a default statement can also be introduced, there must be no duplicate cases too.

Syntax

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

 

Decision Making In Java- Switch Case

Decision Making In Java – Switch case Statement

Example of Switch Case statements in java

// Java program to illustrate switch-case
class SwitchCaseDemo
  {
    public static void main(String args[])
    {
    int i = 9;
    switch (i)
      {
      case 0:
      System.out.println("i is zero.");
      break;
      case 1:
      System.out.println("i is one.");
      break;
      case 2:
      System.out.println("i is two.");
      break;
      default:
      System.out.println("i is greater than 2.");
      }
    }
  }

f.  jump statements in java

Java has three jump statements; they are-

i. Break

ii. Continue

iii. Return

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

i. 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
Decision Making In Java -Break statements

Decision Making In Java – Break Statements

Example of exit statements in java

// Java program to illustrate using break to exit a loop
class BreakLoopDemo
  {
    public static void main(String args[])
    {
     // Initially loop is set to run from 0-9
     for (int i = 0; i < 10; i++)
      {
        // terminate loop when i is 5.
        if (i == 5)
        break;
        System.out.println("i: " + i);
      }
    System.out.println("Loop complete.");
    }
  }
  •  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 t = 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 (t)
          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.");
      }
    }
  }

ii. Continue Statement in Java

This statement is used to continue an iteration of a loop, i.e. to continue for a loop to run with a different condition.Decision Making in java

Decision Making in java – continue Statement in Java

Example of continue statements in java

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

iii. Java Return Statement

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

Example of return statements in java

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

4. Conclusion

In this tutorial for Decision Making in Java, we learned about the basic loops in Java, how they are iterated with their example and Syntax, this will help us understand and code better in the further tutorials. If you have a query, feel free to ask in the comment box

Refer Java Books to learn Java.

For reference