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

Decision Making in Java topic is fairly self explanatory. Decision making means the action of taking decisions and choosing the action plan accordingly.

Consider a real life example, you are driving a car on a road to a concert, and you see your fuel light flashing, you immediately decide that you will stop at the next petrol pump. But if the light would not have blinked at all, you would have continued driving. Right? That is where you took a decision and planned your action.

In this Java Tutorial, we will learn more about decision making in java.

Decision Making in Java

Keeping you updated with latest technology trends, Join DataFlair on Telegram

Decision Making in Java

Decision making in Java executes a particular segment of code based on the result of a boolean condition.It is important because conditions define the flow of programs and the output of a particular program.

The decision making principles in Java chiefly consist of if else statements, continue, break and switch statements. It decides the flow of the program control during the execution of the program.

There are the 6 ways of exercising decision making in Java:
1. if
2. if-else
3. nested-if
4. if-else-if
5. switch-case
6. jump-break,continue,return

1. If Statement in Java

Java if statement is the simplest decision making statement. It encompasses a boolean condition followed by a scope of code which is executed only when the condition evaluates to true. However if there are no curly braces to limit the scope of sentences to be executed if the condition evaluates to true, then only the first line is executed.

Syntax:

if(condition)
{
//code to be executed
}

Flow diagram for Java if statement:

If statement in java

Example Java program to understand if statement:

package com.dataflair.decisionmaking;
public class IfStatement {
  public static void main(String[] args) {

    System.out.println("Understanding if statements.");
    String s = "DataFlair";
    if (s.equals("DataFlair")) {
      System.out.println("The string is DataFlair");
    }
  }
}

Output

Understanding if statements.
The string is DataFlair

2. if else statement in Java

This pair of keywords is used to divide a program to be executed into two parts, one being the code to be executed if the condition evaluates to true and the other one to be executed if the value is false.

However if no curly braces are given the first statement after the if or else keyword is executed.

if(condition)
{
//code to be executed if the condition is true
}
else
{
//code to be executed if the condition is false
}

Flow Diagram of java if-else statement:

if else statement in java

Java program to illustrate the use of if else statement:

package com.dataflair.decsionmaking;
public class IfElseStatement {
  public static void main(String[] args) {

    System.out.println("Understanding if statements.");
    String s = "DataFlairJava";
    if (s.equals("DataFlair")) {
      System.out.println("The string is DataFlair");
    }
    else {
      System.out.println("The string is not DataFlair");
    }
  }
}

Output

The string is not DataFlair

3. Nested if Statements in Java

The nested if is similar to the nested loops we learnt about in the previous chapters. If the condition of the outer if statement evaluates to true then the inner if statement is evaluated.
Nested if’s are important if we have to declare extended conditions to a previous condition

Syntax:

if(condition)
{
//code to be executed
if(condition)
{
//code to be executed
}
}

Flow diagram of Java nested if statement:

nested if statement in java

Java program to illustrate the use of nested if:

package com.dataflair.decisionmaking;
public class IfNestedStatement {
  public static void main(String[] args) {

    System.out.println("Understanding if statements.");
    String s = "DataFlair";
    if (s.equals("DataFlair")) {
      System.out.println("The string is DataFlair");
      if (s.charAt(0) == 'D') {
        System.out.println("The first character is D! ");
      }
    }
    else {
      System.out.println("The string is not DataFlair");
    }
  }
}

Output

Understanding if statements.
The string is DataFlair
The first character is D!

4. if-else-if Statements in Java

These statements are similar to the if else statements . The only difference lies in the fact that each of the else statements can be paired with a different if condition statement. This renders the ladder as a multiple choice option for the user. As soon as one of the if conditions evaluates to true the equivalent code is executed and the rest of the ladder is ignored.

Syntax:

if
{
//code to be executed
}
else if(condition)
{
//code to be executed
}
else if(condition)
{
//code to be executed
}
else
{
//code to be executed
}

Flow Diagram of Java if else if statements:

if else if statement in java

Java program to illustrate the use of if-else-if statements:

package com.dataflair.decisionmaking;
public class IfElseIfStatement {
  public static void main(String[] args) {

    System.out.println("Understanding if statements.");
    String s = "DataFlairPython";
    if (s.equals("DataFlair")) {
      System.out.println("The string is DataFlair");
    }
    else if (s.equals("DataFlairJava")) {
      System.out.println("The string is DataFlair and course is Java");
    }
    else if (s.equals("DataFlairPython")) {
      System.out.println("The string is DataFlair and the course is Python");
    }
  }
}

Output

Understanding if statements.
The string is DataFlair and the course is Python

5. Switch Statement in Java

Java switch statement is a different form of the if else if ladder statements.

  • This saves the hassle of writing else if for every different option.
  • It branches the flow of the program to multiple points as and when required or specified by the conditions.
  • It bases the flow of the program based on the output of the expression.
  • As soon as the expression is evaluated, the result is matched with each and every case listed in the scope. If the output matches with any of the cases mentioned, then the particular block is executed. A break statement is written after every end of the case block so that the remaining statements are not executed.
  • The default case is written which is executed if none of the cases are the result of the expression. This is generally the block where error statements are written.

Syntax:

switch(expression)
{
case <value1>:
//code to be executed
break;
case <value2>:
//code to be executed
break;
default:
//code to be defaultly executed
}

Flow Diagram of Java switch statement:

switch statement in java

Java program to illustrate the switch statement:

package com.dataflair.decisionmaking;
import java.util. * ;
public class SwitchStatement {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System. in );
    System.out.println("You have two options of courses at DataFlair");
    System.out.println("1.Java");
    System.out.println("2.Python");
    System.out.println("Enter the number of course:");
    int ch = sc.nextInt();
    switch (ch) {
    case 1:
      System.out.println("Congrats you have chosen Java!");
      break;
    case 2:
      System.out.println("Congrats you have chosen Python!");
      break;
    default:
      System.out.println("Wrong input!");
      break;
    }

  }
}

Output

You have two options of courses at DataFlair
1.Java
2.Python
Enter the number of course:
2
Congrats you have chosen Python!

6. Jump Statements in Java

The jump statements are the keywords in Java which have specific meaning and specific action of disrupting the normal flow of the program. Some of them are:

a. Java break statement

The break statement , as the name signifies, is useful to break out of the normal workflow of a program. We can use it to terminate loops, end cases of switch statements, and as a goto statement. Break statements, if used in a nested loop, terminates the innermost loop. The outermost loop still functions.

break statement as a goto statement

If we mention a label after the break keyword, the control shifts to the end of the particular label scope. Java prohibits the use of goto statements because it encourages branched behaviour of programming. However break labels function like goto statements.

Flowchart of Java break statement:

break statement in java

Java program to illustrate the use of break keyword:

package com.dataflair.decisionmaking;
import java.util. * ;
public class IfStatement {
  public static void main(String[] args) {
    for (int i = 0; i < 10; i++) {
      System.out.println(i);
      if (i == 7) break;
    }
  }
}

Output

0
1
2
3
4
5
6
7

b. Continue Statement in Java

The continue statement is useful for early iteration of a particular loop. Sometimes rather than breaking out of a loop and halting it for good we want to skip some iterations based on our requirements of a program. This results in the usage of continue statements.

Flow Diagram for Java continue statement:

continue statement in Java

Java program to illustrate the use of the continue statement:

package com.dataflair.decisionmaking;
import java.util. * ;
public class ContinueStatement {
  public static void main(String[] args) {
    for (int i = 0; i < 10; i++) {

      if (i == 7) continue;
      System.out.println(i);

    }
  }
}

Output

0
1
2
3
4
5
6
8
9

Notice that 7 did not print because the entire iteration of it was skipped.

c. Return Statement in Java

This keyword, when executed by the compiler, returns the control back to the method it was called from. This is generally used in methods which are not of the void type and return some values. When the statement is executed, the return function returns to the caller method along with whatever variable is mentioned in the definition.

However, if we mention return statement in the main method, the compiler halts and the program comes to an end. None of the statements below the return statement are executed by the compiler.

Syntax:

return <value>;
or
return;

Java program to illustrate the use of the return statement in Java:

package com.dataflair.decisionmaking;
import java.util. * ;
public class ReturnStatement {
  static int funcsqr(int x) {
    return x * x;
  }
  public static void main(String[] args) {
    for (int i = 0; i < 10; i++) {

      int y = funcsqr(5);
      System.out.println("The value returned by the function is " + y);
      return;
      // System.out.println("This statement would never get executed. ");
    }
  }
}

Output

The value returned by the function is 25

Summary

We learnt about decision making in Java. These statements are very important in framing algorithms and a clear concept of the decision statements is of utmost importance in programming.

Hope you enjoyed the article. Do share your feedback in the comment section!!!

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.