What are Control Flow Statements in Java?

In this tutorial, we will understand the answer of a question i.e What are Control Flow Statements in Java? We will also focus on their different types as well as their working.

What are Control Flow Statements in Java?

Control Flow Statements are used to control the flow of execution of a program. These statements are special statements that need not be executed sequentially. Every Control Flow Statement has a condition according to which the flow of the program changes.what are control flow statements in javaJava has three basic types of Control Flow Statements:

  1. if statement
  2. if-else statement
  3. switch statement

What is an if-statement?

if-statement is the simplest of all the other conditional statements. It has a condition and a body. The condition is evaluated and then it decided whether or not the code in the body should be executed or not.

if-statement in java

Syntax:

if(condition) {
    //Body
}
next statement;

At first, the condition is evaluated to get a Boolean result. The Boolean result plays a key role in whether or not the body should be executed or not. If the condition is evaluated as ‘true’ then the body of the if-statement is executed and then it continues to execute the next statement after it. If the condition is ‘false’, then the body isn’t executed and then the next statement after the if-statement gets executed.
This way, the flow of the program is controlled and the key role played here is by the condition.

Example:

class Program {
    public static void main(String agmt[]) {
        if(2<4) {
            System.out.println(“2 is less than 4”);
        }
    }
}

/* Output
2 is less than 4
*/

In the above example, the condition in the if-statement is 2< 4 which will return a Boolean result as ‘true’ (because 2 is less than 4). This means the condition is evaluated and it is ‘true’, then the body of the if-condition is executed and the console gets printed with “2 is less than 4”.

What is an if-else statement?

The if-else statement is an extended part of the if-statement. It has all the things that an if-statement has but has an extra ‘else’ block.

if-else statement in java

Syntax:

if(condition) {
    //Executed when condition is true
}
else {
    //Executed when the condition is false
}

When the condition in the if-statement gets evaluated as ‘true’ then the body next to the if-block gets executed. In case where the condition gets evaluated as ‘false’ then the body of the else block gets executed.

Example:

class Program {
    public static void main(String agmt[]) {
        if(24 == 47) {
            System.out.println(“24 is equal to 47”);
        }
        else {
            System.out.println(“24 is not equal to 47”);
        }
    }
}

/* Output
24 is not equal to 47
*/

In the above example, the condition 24 == 47 gets evaluated to “false” and the body next to if-condition is not executed. But we also have an ‘else’ block right after the if block, so the else block gets executed and the console gets printed with “24 is not equal to 47”.

What is a switch statement?

The switch statement is another conditional statement that is also used to control the flow of a program. A switch statement has an expression (which evaluates to a value) and also has its block of statements arranged according to the result of the expression.

switch statement in java

Syntax:

switch(expression) {
    case &amp;amp;lt;case_1&amp;amp;gt;: //Code
          break;
    case &amp;amp;lt;case_2&amp;amp;gt;: //Code
          break;
    ................
    case &amp;amp;lt;case_n&amp;amp;gt;: //Code
          break;
    default: //Code
}

The code inside the switch block is split into cases. When the expression is evaluated in the beginning, it will result in one the case that is mentioned inside the switch block. If neither of the cases match with the evaluated expression, then the statements after the ‘default’ block gets executed.

Example:

class Program {
    public static void main(String agmt[]) {
        switch(4+26+36) {
            case 65: System.out.println(“The value is 65”);
                     break;
            case 68: System.out.println(“The value is 68”);
                     break;
            case 66: System.out.println(“The value is 66”);
                     break;
            default: System.out.println(“I don’t know what it is”);
        }
    }
}

The above example clearly shows how the code is split into cases in a switch statement. The expression that is passed into the switch is 4+26+36. This expression, when calculated, will give 66. The value 66 will enter the body of the switch and check if there is a case with the value 66.
In the example, we see the third case matches with the outcome of the expression. So, the statements after the ‘case 66’ will get executed. In this case, we have a printing statement that prints “The value is 66”.

Note: We also see a break-statement at the end of every case. This is placed for a reason. Suppose, the first case matches with the expression of the switch statement, then the code written in the first case will get executed but then if there is no break-statement after that the code after the first case will continue to executes and then the next one and so on. The switch-statement will break only if it finds a break-statement or if it reaches the end of the switch case.