Home Java Tutorial Java Flow Control Statements

Java Flow Control Statements

by anupmaurya
4 minutes read

In this article, you’ll learn about Java Flow Control Statements which includes If-else Statement, Switch Statement, While Loop, Do While and more.

Java application code is normally executed sequentially from top to bottom in the order that the code appears. To apply business logic, we may need to execute code on conditional basis. Control flow statements helps in this conditional execution of code blocks.

All control flow statements are associated with a business condition – when true, the code block executes; when false it is skipped.

Java supports following control statements.

1. If-else Statement

If-else statement tells the program to execute a certain section of code only if a particular test evaluates to true otherwise else block is executed.

We can have nested if-else blocks.

public class JavaExample 
{
    public static void main(String[] args) 
    {
        boolean condition = true;
         
        if(condition) {
            System.out.println("Condition is true");
        } 
        else
        {
            System.out.println("Condition is false");
        }
    }
}

Program output.

Condition is true

2. Switch Statement

As if-else statement tells your program to execute a certain section of code only if a particular test evaluates to true or false, the switch statement can have multiple execution paths.

A switch works with the byte, short, char, and int primitive data types. It also works with enumerated types, the String class, and a few special classes that wrap certain primitive types: Character, Byte, Short, and Integer. (enums were added in java 5, and String class was added in java 7).

public class JavaExample 
{
    public static void main(String[] args) 
    {
        String value = "B";
 
        switch (value) 
        {
            case "A":
                System.out.println("Value is A");
                break;
            case "B": 
                System.out.println("Value is B");
                break;
            default:
                System.out.println("Value is neither A nor B");
                break;
        }
    }
}

Program output.

Value is B

3. While Loop

The while statement or loop continually executes a block of statements while a particular condition is true. The while statement continues testing the expression and executing its block until the expression evaluates to false.

public class JavaExample 
{
    public static void main(String[] args)
    {
        int count = 1;
        while (count < 5) 
        {
            System.out.println("Count is: " + count);
            count++;
        }
    }
}

Program output.

1
2
3
4
5 

4. Do-while Loop

The difference between do-while and while is that do-while evaluates its expression at the bottom of the loop instead of the top. Therefore, the statements within the do block are always executed at least once.

Note that the do-while statement ends with a semicolon. The condition-expression must be a boolean expression.

int i = 1;
int sum = 0;
 
do
{
    sum = sum + i;
    i++;
}
while (i <= 10);
 
System.out.println(sum);

Program output.

  55 

5. For Loop

The for statement iterates over a range of values. It repeatedly loops over values until a particular condition is satisfied.

for(int num = 1; num <= 5; num++)
{
 
     System.out.println(num);
 
}

Program output.

12345

6. Enhanced For-each Loop

Java 5 introduced an foreach loop, which is called a enhanced for-each loop. It is used for iterating over elements of arrays and collections.

int[] numList = {10, 20, 30, 40};
 
for(int num : numList) 
{
    System.out.println(num);
}

Program output.

10
20
30
40

7. Labeled Statement

Whenever during a program execution, a labeled break statement is encountered that control immediately goes out of enclosing labeled block. Similarly, labeled continue will bring control back to start. Just like in normal break and continue statements, with additional names given to blocks.

public class JavaExample 
{
    public static void main(String[] args) 
    {
        loop: for(int i=0; i < 6; i++) 
        {
            if(i % 2 == 0) 
            {
                System.out.println("In if block :: " + i);
                continue loop;
            } 
            else
            {
                System.out.println("In else block :: " + i);
            }
        }
    }
}

Program output.

In if block :: 0
In else block :: 1
In if block :: 2
In else block :: 3
In if block :: 4
In else block :: 5

You may also like

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.