Skip to main content

Primary tabs

Submitted by admin on 9 February 2023
Let us first understand what is Statements in the computer programming language before going in depth into control statements..

A statement in a programming language is a single command or instruction that specifies some action to be performed by the computer. In other words, it's a unit of code that tells the computer what to do. Statements can range from simple commands like assigning a value to a variable, to complex logic structures such as if-else statements or loops.


For example, the following is a statement in Java: int x = 5;

Introduction to Control Statements in Java

In Java, control statements are used to control the flow of execution of a program. They allow the program to make decisions based on certain conditions and execute a block of code accordingly. There are three types of control statements in Java:

if statement: Executes a block of code if a specified condition is true.


int x = 10;
if (x > 5) {
    System.out.println("x is greater than 5");
}

if-else statement: Executes a block of code if a specified condition is true, and another block of code if it's false.


int x = 10;
if (x > 5) {
    System.out.println("x is greater than 5");
} else {
    System.out.println("x is not greater than 5");
}


else-if statement: Executes a block of code if a multiple condition is true, and another block of code if it's false.


int x = 10;
if (x > 15) {
    System.out.println("x is greater than 15");
} else if (x > 10) {
    System.out.println("x is greater than 10 but not greater than 15");
} else {
    System.out.println("x is not greater than 10");
}


switch statement: Selects one of many blocks of code to be executed based on a specified condition.


int day = 3;
switch (day) {
    case 1:
        System.out.println("Monday");
        break;
    case 2:
        System.out.println("Tuesday");
        break;
    case 3:
        System.out.println("Wednesday");
        break;
    default:
        System.out.println("Invalid day");
}

2. Loop statements:

for-each loop: Executes the data structures like array or collection.

for loop: Executes a block of code for a specified number of times.

while loop: Executes a block of code repeatedly until a specified condition is false.

do-while loop: Executes a block of code repeatedly until a specified condition is false, but ensures that the code is executed at least once.

3. Jump statements:


int day = 3;
switch (day) {
    case 1:
        System.out.println("Monday");
        break;
    case 2:
        System.out.println("Tuesday");
        break;
    case 3:
        System.out.println("Wednesday");
        break;
    default:
        System.out.println("Invalid day");
}



break statement: Terminates a loop or switch statement and transfers control to the next statement.


int day = 3;
switch (day) {
    case 1:
        System.out.println("Monday");
        break;
    case 2:
        System.out.println("Tuesday");
        break;
    case 3:
        System.out.println("Wednesday");
        break;
    default:
        System.out.println("Invalid day");
}

continue statement: Skips the current iteration of a loop and continues with the next iteration.



int day = 3;
switch (day) {
    case 1:
        System.out.println("Monday");
        continue;
    case 2:
        System.out.println("Tuesday");
        continue;
    case 3:
        System.out.println("Wednesday");
        continue;
    default:
        System.out.println("Default day");
}



return statement: Terminates a method and returns a value to the calling method.


return expression;


Tags