Decision Making in Java – if, if-else, switch, break, continue, jump

What is decision-making in Java?

Decision-making in Java is a structure that has one or more conditions that need to be evaluated or tested by the program.

Along with a statement or statements that need to be run if the condition is true, and, if desired, other statements that need to be run if the condition is false.

Why is Decision Making Important in Java?

Decision-making in Java is an important concept for programmers to understand because it defines one or more conditions.

That will be evaluated or tested by the program, along with a statement or statements that will be executed.

If the condition is true, and optionally, additional statements that will be executed if the condition is false.


The execution of a program can be controlled by a programming language using control statements based on specific conditions.

These are employed to cause the execution flow to advance and branch in response to changes in a program’s state.

Decision Making Selection Statements In Java

  • if
  • if-else
  • if-else-if
  • switch-case
  • jump – break, continue and return

Decision Making In Java Programming

If Statement In Java

The If Statement In Java is the simplest way to make a Decision Making In Java. It is used to decide if a statement or group of statements will be run or not.

For example, if a certain condition is true, a block of statements will be run, but if it is false, the block of statements will not be run.

if statement syntax

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

Here, the result of evaluating the condition will be either true or false.

If statement works with boolean values. If the value is true, it will run the block of statements under it.

If we don’t put the curly braces “{ and }” after if( condition ), then by default the if statement will consider the next one statement to be inside its block.

As one example:

if(condition)
statementOne;
statementTwo;
// Here if the condition is true, if block
// will consider only statement1 to be inside
// its block.
// end

 Decision Making In Java If Statement
Decision Making In Java If Statement
if statement In Java Example Program
// Java program to illustrate If statement
public 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 Glenn Magada Azuelo");
    }
}

Output:

I am Glenn Magada Azuelo


In order for you to test the Java code provided in this lesson, you must test the code in your code editor.

You can test the above example here! ➡Java Online Compiler 


If-Else Statement In Java

The if statement tells us that a block of statements will be run if a condition is true, but not if the condition is false.

But what if we want to do something different if the condition is false?

Here comes the “or else” clause. When the condition is false, we can run a block of code with the else statement and the if statement.

if-else statement syntax

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

Decision Statement In Java If-Else Statement
Decision Statement In Java If-Else Statement
if-else statement in Java Example Program
// Java program to illustrate if-else statement
public class IfElseDemo {
    public static void main(String args[])
    {
        int g = 10;

        if (g < 15)
            System.out.println("g is smaller than 15");
        else
            System.out.println("g is greater than 15");
    }
}

Output:

g is smaller than 15

You can test the above example here! ➡Java Online Compiler 


Nested-If Statement In Java

A Nested-If is an if statement that is the subject of another if or else. Nested if statements have an if statement inside an if statement.

Yes, Java lets us put if statements inside of each other. I.e., we can put an if statement inside another if statement.

nested-if statement syntax

if (conditionOne){
// Executes when conditionOne is true
if (conditionTwo){
// Executes when conditionTwo is true
}
}

Decision Making In Java Nested-If Statement
Decision Making In Java Nested-If Statement

Nested-If Statement In Java Example Program
// Java program to illustrate nested-if statement
// program by Glenn Magada Azuelo
public class NestedIfDemo {
    public static void main(String args[])
    {
        int g = 11;

        if (g == 11) {
            // First if statement
            if (g < 15)
                System.out.println("g is smaller than 16");

            // Nested - if statement
            // Will only be executed if statement above
            // it is true
            if (g < 12)
                System.out.println(
                    "g is smaller than 13 too");
            else
                System.out.println("g is greater than 16");
        }
    }
}

Output:

g is smaller than 16
g is smaller than 13 too

You can test the above example here! ➡Java Online Compiler 


If-Else-If Ladder In Java

The if-else-if ladder in Java is used to choose between different options. The if statements run from the top to the bottom.

When one of the conditions that control the if is true, the statement that goes with that if is run, and the rest of the ladder is skipped.

if-else-if ladder statement syntax

if (conditionOne)
statement;
else if (conditionTwo)
statement;
.
.
else
statement;

Decision Making In Java If-Else Ladder
Decision Making In Java If-Else Ladder
If-else-If ladder in Java example program
// Java program to illustrate if-else-if ladder
// program by Glenn Magada Azuelo
public class ifelseifDemo {
    public static void main(String args[])
    {
        int g = 21;

        if (g == 11)
            System.out.println("g is 10");
        else if (g == 15)
            System.out.println("g is 15");
        else if (g == 21)
            System.out.println("g is 21");
        else
            System.out.println("g is not present");
    }
}

Output:

g is 21

You can test the above example here! ➡Java Online Compiler 


Switch-Case In Java

The switch statement or switch case is a branching statement in Java.

Depending on the value of the expression, different sections of code can be run quickly.

The given expression can have a primitive data type like int, char, short, byte, or char.

switch-case statement syntax

switch (expression)
{
case valueOne:
statementOne;
break;
case valueTwo:
statementTwo;
break;
.
.
case valueN:
statementN;
break;
default:
statementDefault;
}

  • The type of the expression can be byte, short, int char, or enumeration. Expressions can also be of type String as of JDK 7.

  • There can’t be duplicate case values.

  • The default statement isn’t required.

  • Inside of a switch, the break statement is used to end a sequence of statements.

  • The break statement is an option. If left out, execution will move on to the next case.

Jump In Java

The Jump statements are used to move control of the program from one point to another point in the program.

Jump statements are mostly used to stop a loop or switch-case immediately.

Java offers three decision making jump statements: break, continue, and return.

break In Java

The Break Statement In Java is used to terminate the loop immediately.

When a break statement is found inside a loop, the loop stops iterating and control goes back to the first statement after the loop.

  • In a switch statement, you can terminate a sequence (discussed above).
  • For a loop to end.
  • Used as a “civilized” version of goto.

continue in java

The Continue Statement In Java is used to end the current iteration in a for loop (or a while loop) and move on to the next iteration.

It can be helpful to force an early iteration of a loop. That is, you might want to keep running the loop but stop running the rest of the code in its body for this particular iteration.

This is, in effect, a goto just past the body of the loop, to the end of the loop. The continue statement does this action.

Decision Making In Java Continue Statement
Decision Making In Java Continue Statement
Continue In Java Example Program
// Java program to illustrate using
// continue in an if statement
// program by Glenn Magada Azuelo
public 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 % 5 == 0)
                continue;

            // If number is odd, print it
            System.out.print(i + " ");
        }
    }
}

Output:

1 2 3 4 6 7 8 9

You can test the above example here! ➡Java Online Compiler 

return in java

The Return Statement In Java is used to give back a value when the block is done running.

If you put a return statement inside a loop, the loop will break, and the compiler will not pay attention to any other statements.

Return In Java Example Program
// Java program to illustrate using return
public class Return {
    public static void main(String args[])
    {
        boolean g = true;
        System.out.println("Before the return.");

        if (g)
            return;

        // Compiler will bypass every statement
        // after return
        System.out.println("This won't execute.");
    }
}

Output:

Before the return.

You can test the above example here! ➡Java Online Compiler 


Conclusion

In this article, we talked about Decision Making In Java, how it works, and how this chapter could help you learn Java.

This could be one of the most important learning guides that will help you improve your skills and knowledge as a future Java Developer.


Leave a Comment