Control Flow Statements in Java

In computer science, control flow (or flow of control) is the order in which individual statements, instructions or function calls of an imperative program are executed or evaluated.

  1. if then else Statement
  2. Switch Statement
  3. for Statement
  4. while Statement
  5. do-while Statement

If then else statement

If then else statement to specify a block of Java code to be executed if a condition is true .

public class ControlFlowStatements {
    public static void main(String[] args) {

        // If then else Statement
        boolean value = false;
        if (value) {
            System.out.println("Value is " + value);
        } else if (!value) {
            System.out.println("Value is " + value);
        }
        else {
            System.out.println("Value is not boolean");
        }
    }
}
Value is false

Switch Statement

Switch statement to select one of many code blocks to be executed.

public class ControlFlowStatements {
    public static void main(String[] args) {

        // Switch Statement
        int value = 1;
        switch(value){
            case 1:
                System.out.println("Value is 1");
                break;
            case 2:
                System.out.println("Value is 2");
                break;
            case 3:case 4: case 5:
                System.out.println("Value is 3 or 4, or 5");
                break;
            default:
                System.out.println("Value is not 1,2,3,4,5");
                break;
        }

    }
}
Value is 1
  • Breaks are vital to close the case comparison
  • Switch value is able to use the primitive types byte, short,char, and int
  • After JDK version 7 String can use as Switch value.

for Statement

for statement is used to loop some code so in other words it is going to allow us to execute code a certain number of times.

public class ControlFlowStatements {
    public static void main(String[] args) {

        // for loop print 0 to 4
        for (int i = 0; i < 5; i++){
            System.out.println(i);
        }
    }
}
0
1
2
3
4

while Statement

While statement (loop) will execute the code block until the condition is false.

The while loop check the condition at the start before executing the the code block

public class ControlFlowStatements {
    public static void main(String[] args) {

        // while loop print 0 to 4
        int i = 0;
        while (i < 5){
            System.out.println(i);
            i++;
        }
    }
}
0
1
2
3
4

Use break;

The break keyword can be used in any of the loop control structures. It causes the loop to immediately stop while loop.

public class ControlFlowStatements {
    public static void main(String[] args) {

        // while loop print 0 to 4
        int i = 0;
        while (true){
            if (i == 5){
                break;
            }
            System.out.println(i);
            i++;
        }
    }
}
0
1
2
3
4

Use continue;

The continue keyword can be used in any of the loop control structures. It causes the loop to immediately jump to the next iteration of the loop.

public class ControlFlowStatements {
    public static void main(String[] args) {

        // while loop print 0,1,2,4 without 3
        int i = 0;
        while (i < 5) {
            if (i == 3) {
                i++;
                continue;
            }
            System.out.println(i);
            i++;
        }
    }
}
0
1
2
4

If you want to use true as while loop condition, be careful to Infinite loop.

do-while Statement

do while loop will always execute the code block at least once and also execute the code block until the condition is false.

do while loop the code block is executed at least once then the condition is checked.

public class ControlFlowStatements {
    public static void main(String[] args) {

        // do-while loop print 0 to 4
        int i = 0;
        do {
            System.out.println(i);
            i++;
        }while(i < 5);
    }
}
0
1
2
3
4
  • do-while loop while( i < 5); semicolon is needed.

Leave a Reply

Your email address will not be published.

ANOTE.DEV