Control structures in Java

A program is a list of instructions or blocks of instructions. Java provides Control structures that can change the path of execution and control the execution of instructions.
Here is the table content of the article will we will cover this topic.
1. Three kinds of control structures in Java?
i) if statement
ii) nested if statement
iii) if-else statement
iv) if-else if statement/ ladder if statements
v)switch statement
2. Control statements/Conditional Branches?
3. Loops in Java?
4. Branching Statements in Java?

Three kinds of control structures in Java

1. Control statements/Conditional Branches

These are used to choose the path for execution. There are some types of control statements:

  1. if statement: It is a simple decision-making statement. It is used to decide whether the statement or block of statements should be executed or not. Block of statements will be executed if the given condition is true otherwise the block of the statement will be skipped. The if statmenet always accepts the boolean value and perform the action accordungly. Here we will see the simplest example of if statement. We will discuss it in details in a seprate post. Visit here for more details.
public class IfStatementExample
{
    public static void main(String arg[]) {
        int a = 5;
        int b = 4;
        // Evaluating the expression that will return true or false
        if (a > b) {
            System.out.println("a is greater than b");
        }
    }
}

Output: a is greater than b

  1. nested if statement: Nested if statements mean an if statement inside an if statement. The inner block of if statement will be executed only if the outer block condition is true. This statmenet is useful when you want to test a dependent condition. Here we will see the example of nested if with a common example and see how does it work. Visit here for more details.
public class NestedIfStatementExample
{
    public static void main(String arg[])
    {
        int age = 20;
        boolean hasVoterCard = true;
        // Evaluating the expression that will return true or false
        if (age > 18)
        {
            // If outer condition is true then this condition will be check
            if (hasVoterCard)
            {
                System.out.println("You are Eligible");
            }
        }
    }
}

Output: You are Eligible

  1. if-else statement: In if statement the block of statements executes when the given condition is true otherwise block of the statement get skipped. Suppose when we want to execute the some block of code if the condition is false. Here we come on the if-else statement. An if-else statement, there are two blocks one is if block and another is else block. If a certain condition is true, then if block executes otherwise else block executes. Let’s see the example the example and understand it. Visit here for more details.
public class IfElseStatementExample
{
    public static void main(String arg[]) {
        int a = 10;
        int b = 50;
        // Evaluating the expression that will return true or false
        if (a > b)
        {
            System.out.println("a is greater than b");
        }
        else
        {
            System.out.println("b is greater than a");
        }
    }
}

Output: b is greater than a

  1. if-else if statement/ ladder if statements: If we want to execute the different codes based on different conditions then we can use if-else-if. It is also known as if-else if ladder. This statement is always be executed from the top down. During the execution of conditions if any condition founds true, then the statement associated with that if it is executed, and the rest of the code will be skipped. If none of the conditions is true, then the final else statement will be executed. Let’s see the example of ladder if and you can visit here for more details.
public class IfElseIfStatementExample
{
    public static void main(String arg[]) {
        int a = 10;
        // Evaluating the expression that will return true or false
        if (a == 1)
        {
            System.out.println("Value of a: "+a);
        }
        // Evaluating the expression that will return true or false
        else if(a == 5)
        {
            System.out.println("Value of a: "+a);
        }
        // Evaluating the expression that will return true or false
        else if(a == 10)
        {
            System.out.println("Value of a: "+a);
        }
        else
        {
            System.out.println("else block");
        }
    }
}

Output: Value of a: 10

  1. Switch statement: The switch statement is like the if-else-if ladder statement. To reduce the code complexity of the if-else-if ladder switch statement comes.
    In a switch, the statement executes one statement from multiple statements based on condition. In the switch statements, we have a number of choices and we can perform a different task for each choice. You can read it in detail, for more details.
public class SwitchStatementExample
{
    public static void main(String arg[]) {
        int a = 10;
        // Evaluating the expression that will return true or false
        switch(a)
        {
            case 1:
                System.out.println("Value of a: 1");
                break;
            case 5:
                System.out.println("Value of a: 5");
                break;
            case 10:
                System.out.println("Value of a: 10");
                break;
            default:
                System.out.println("else block");
                break;
        }
    }
}

Output: Value of a: 10

2. Loops in Java

The loops are used to iterate the instruction multiple times. When we want to execute the statement a number of times then we use loops in java.

  1. for loop: A for loop executes the block of code several times. It means the number of iterations is fixed. JAVA provides a concise way of writing the loop structure. When we want to execute the block of code serval time. Let’s see the example and visit here for more details.
public class ForLoopExample
{
    public static void main(String arg[])
    {
        // Creating a for loop
        for(int i = 1; i <= 3; i++)
        {
            System.out.println("Iteration number: "+i);
        }
    }
}

Output: Iteration number: 1
Iteration number: 2
Iteration number: 3

  1. while loop: A while loop allows code to be executed repeatedly until a condition is satisfied. If the number of iterations is not fixed, it is recommended to use a while loop. It is also known as an entry control loop. For more details.
public class WhileLoopExample
{
    public static void main(String arg[])
    {
        // Creating a while loop
        int i = 1;
        while(i <= 3)
        {
            System.out.println("Iteration number: "+i);
            i++;
        }
    }
}

Output: Iteration number: 1
Iteration number: 2
Iteration number: 3

  1. do-while loop: It is like while loop with the only difference that it checks for the condition after the execution of the body of the loop. It is also known as Exit Control Loop. For more details.
public class DoWhileLoopExample
{
    public static void main(String arg[])
    {
        // Creating a while loop
        int i = 1;
        do
        {
            System.out.println("Iteration number: "+i);
            i++;
        }
        while(i <= 3);
    }
}

Output: Iteration number: 1
Iteration number: 2
Iteration number: 3

3. Branching Statements in Java

These are used to alter the flow of control in loops. 

  1. break: If a break statement is encountered inside a loop, the loop is immediately terminated, and the control of the program goes to the next statement following the loop. It is used along with if statement in loops. If the condition is true, then the break statement terminates the loop immediately.
public class BreakStatementExample
{
    public static void main(String arg[])
    {
        for(int i = 0; i <=3; i++)
        {
            System.out.println("Iteration number:" + i);
            if(i == 2)
                break;
        }
    }
}

Output: Iteration number:0
Iteration number:1
Iteration number:2

  1. continue: The continue statement is used in a loop control structure. If you want to skip some code and need to jump to the next iteration of the loop immediately. Then you can use a continue statement. It can be used for loop or while loop. Read it in details
public class ContinueStatementExample
{
    public static void main(String arg[])
    {
        for(int i = 0; i <=3; i++)
        {
            if(i == 1)
                continue;
            System.out.println("Iteration number:" + i);
        }
    }
}

Output: Iteration number:0
Iteration number:2
Iteration number:3

Control Structure of Java

Leave a Comment