Java Control Flow Statements

To plan or decide something before implementation is always essential. This will help execute every step more efficiently, and you have an idea about the action that to be performed next.

In computer language, decision-making is an important part. Predominantly, the execution starts from the top to the bottom. But sometimes, the decision is vital to know which block needs to perform next. Control flow in Java will help you to decide the next action. This article gives detailed information about control flow in Java, which helps execute code sequentially.

Java Control Flow

Java Control Flow Statements

Image Source

Java control flow controls the flow of execution of code in the program. In Java, by placing the decision making, branching, looping, and adding conditional blocks, you can control the code’s flow.

Types of control flow statement classified as:

  1. Decision-Making Statements
  2. Looping Statements
  3. Branching Statements

Decision-Making Statements

Decision Making Statements use when you want to decide which block of code will get executed.

if Statement

The if condition executes the code only if the condition is true; otherwise, the code will not get executed.

Syntax: 

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

Example:

public class Greaternum {
    public static void main(String[] args) {
        int num1 = 1;
        int num2 = 2;
        
        if(num1<num2) {
            System.out.println("The num1 is less than the num2.");
        }
    }
}

Output:

The num1 is less than the num2

If condition is true, thats why the code executes, and we get the output.

if-else Statement

If-else statement similar to the if statement, it only had one extra code block that is else. When if statement condition is true, if block executes otherwise, the else block gets executed.

Example:

public class Greaternum {
    public static void main(String[] args) {
        int num1 = 10;
        int num2 = 5;
        
        if(num1<num2) {
            System.out.println("The num1 is less than the num2.");
        } 
        else {
            System.out.println("The num1 is equal or greater than the num2.");
        }
    }
}

Output:

The num1 is equal to or greater than the num2

We get this output because if condition is false, the else block gets executed, and we get our output.

switch Statement

Till now, you have seen the if-else execution path. Now let’s learn the switch statement.

public class SwitchExample {
    public static void main(String[] args) {
        int rollNum = 3;
        String stuName;
        
        switch(rollNum) {
            case 1: stuName = "Rooli";
                break;
            case 2: stuName = "Nisha";
                break;
            case 3: stuName = "Anjali";
                break;
            case 4: stuName = "Swati";
                break;
            case 5: stuName = "Anisha";
                break;
            default : stuName = "Invalid roll number";
                break;
        }
        
        System.out.println("The name of the student is "+stuName+"!");
    }
}

Output:

The name of the student is Anjali!

We get this output because the third statement gets executed. The break statement terminates the rest of the condition apart from the switch condition.

Looping Statements

In the looping statement, first, we make the decision and then execute the code multiple times.

for Loop

First, you need to check the condition. If the value is true, the piece of code written under the for statement gets executed. After the code block’s execution, the control moves back to the condition if the condition satisfies the block of code executes till the condition reaches out to be false.

Syntax:

for(int variable declaration ; condition ; increment /decrement){
    ----
    //code to be executed
    ----
}

Example:

public class ForExample {
    public static void main(String[] args) {
        for(int i = 0; i<=2; i++) {
            System.out.println(i+". My name is Anjali!");
        }
    }
}

The value of i initializes to zero, and the condition imparts that i need to be executed two times. And after the execution of each condition, the value of i gets incremented till the condition becomes false.

Output:

0. My name is Anjali!
1. My name is Anjali!
2. My name is Anjali!

while Loop

Condition and increment or decrement statements are similar to for loop, but the syntax is quite different.

Syntax:

int variable declaration

while (condition) {
    //code to be executed
    //increment or decrement statement
}

Example:

public class WhileExample {
    public static void main(String[] args) {
        int i = 0;
        
        while(i<=2) {
            System.out.println(i+". My name is Anjali!");
            i++;
        }
    }
}

Three times execution takes place, i.e., 0,1 and 2. The increment or decrement operator is written after the code. First, the condition will get the check after the code starts its execution, and then the value gets increment. The incremented or decremented value checks the condition; if the condition is true, the code’s execution begins.

Output:

0. My name is Anjali!
1. My name is Anjali!
2. My name is Anjali!

do-while Loop

In the do-while loop, it firsts execute the code then checks the condition.

Syntax:

int variable declaration

do {
    //code to be executed
} while ( condition statement)}

Example:

public class DoWhile {
    public static void main(String[] args) {
        int i = 0;
        
        do {
            System.out.println(i+". My name is Anjali!");
            i++;
        }while (i<=2);
    }
}

Output:

0. My name is Anjali!
1. My name is Anjali!
2. My name is Anjali!

Branching Statements

Three types of branching statements are:

  1. break statement
  2. continue statement
  3. return statement

break Statement

We use a break statement to terminate the flow of execution. We mainly use break statements in switch-case conditions or loop conditions.

Example:

public class BreakExample {
    public static void main(String[] args) {
        for(int i = 0; i<=5; i++) {
            if(i == 4) {
                break;
            }
            System.out.println(i+". My name is Anjali!");
        }
    }
}

Terminates the execution after i=4;

Output:

0. My name is Anjali!
1. My name is Anjali!
2. My name is Anjali!
3. My name is Anjali!

continue Statement

The use of continuous operation is to skip the ongoing iteration and sends the control back to the loop’s condition.

Example:

public class ContinueExp {
    public static void main(String[] args) {
        String inputSttring = "Anjali Singh";
        int count = 0;
        
        for(int i=0; i<inputSttring.length(); i++) {
            if(inputSttring.charAt(i) != 'a') {
                continue;
            }
            count++;
        }
        
        System.out.println("We found 'a' in the inputString '"+inputSttring+"', "+count+ " times.");
    }
}

This program is the find the count of occurrence of “a” in the given string.

In for loop, we use the continue statement to find the letter “a,” if the current iteration is not “a,” then, continue statement will skip the operation and send the control back to the loop.

Output:

We found ‘a’ in the inputString ‘Anjali Singh’ three times.

return Statement

The return statement is found at the bottom of the program. It simply exits from method code. The control flows go to the line from where the method is invoked.

Example:

public class returnExample {
  static int method1(int x) {
    return 4 + x;
  }

  public static void main(String[] args) {
    System.out.println(method1(5));
  }
}

Output:

9

Leave a Comment

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