Log In

Don't have an account? Sign up now

Lost Password?

Sign Up

Prev Next

Control Flow Statements

Control statements in Java allow a program to make decisions, repeat tasks, and control the flow of execution based on conditions. Without control statements, a Java program would execute line by line with no flexibility. By using conditional statements and loops, developers can build intelligent, dynamic, and efficient programs.


1. if Statement

Description

The if statement is used to execute a block of code only when a specific condition is true. If the condition evaluates to false, the code inside the if block is skipped.

Syntax

if (condition) {
    // code to execute
}

Example

int age = 20;

if (age >= 18) {
    System.out.println("You are eligible to vote.");
}

Explanation

If the value of age is 18 or above, the message is printed. Otherwise, nothing happens.


2. if-else Statement

Description

The if-else statement provides two execution paths. One block runs if the condition is true, and the other runs if the condition is false.

Syntax

if (condition) {
    // true block
} else {
    // false block
}

Example

int marks = 45;

if (marks >= 50) {
    System.out.println("Pass");
} else {
    System.out.println("Fail");
}

Explanation

If marks are 50 or above, the student passes; otherwise, the student fails.


3. Nested if Statement

Description

A nested if statement is an if statement inside another if. It is used when multiple conditions must be checked in a specific order.

Syntax

if (condition1) {
    if (condition2) {
        // code
    }
}

Example

int age = 22;
boolean hasID = true;

if (age >= 18) {
    if (hasID) {
        System.out.println("Entry allowed.");
    }
}

Explanation

The message is printed only if both conditions are satisfied.


4. switch-case Statement

Description

The switch-case statement is used to execute one block of code from multiple choices based on the value of a variable. It is often used as an alternative to long if-else-if chains.

Syntax

switch (expression) {
    case value1:
        // code
        break;
    case value2:
        // code
        break;
    default:
        // code
}

Example

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");
}

Explanation

The program prints the day corresponding to the given number. The break statement prevents fall-through.


5. Ternary Operator

Description

The ternary operator is a short-hand alternative to if-else. It evaluates a condition and returns one of two values.

Syntax

condition ? value_if_true : value_if_false;

Example

int number = 10;

String result = (number % 2 == 0) ? "Even" : "Odd";
System.out.println(result);

Explanation

If the number is divisible by 2, it prints “Even”; otherwise, it prints “Odd”.


6. for Loop

Description

The for loop is used when the number of iterations is known in advance. It consists of initialization, condition, and increment/decrement.

Syntax

for (initialization; condition; increment/decrement) {
    // code
}

Example

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

Explanation

The loop prints numbers from 1 to 5.


7. while Loop

Description

The while loop executes a block of code as long as the condition is true. The condition is checked before each iteration.

Syntax

while (condition) {
    // code
}

Example

int i = 1;

while (i <= 5) {
    System.out.println(i);
    i++;
}

Explanation

The loop prints numbers from 1 to 5.


8. do-while Loop

Description

The do-while loop executes the code at least once, even if the condition is false, because the condition is checked after execution.

Syntax

do {
    // code
} while (condition);

Example

int i = 1;

do {
    System.out.println(i);
    i++;
} while (i <= 5);

Explanation

The loop prints numbers from 1 to 5, guaranteeing at least one execution.


9. break Statement

Description

The break statement is used to terminate a loop or switch statement immediately.

Example

for (int i = 1; i <= 5; i++) {
    if (i == 3) {
        break;
    }
    System.out.println(i);
}

Explanation

The loop stops when i becomes 3.


10. continue Statement

Description

The continue statement skips the current iteration of the loop and moves to the next iteration.

Example

for (int i = 1; i <= 5; i++) {
    if (i == 3) {
        continue;
    }
    System.out.println(i);
}

Explanation

The number 3 is skipped, and the loop continues with the next value.

Leave a Comment

    🚀 Join Common Jobs Pro — Referrals & Profile Visibility Join Now ×
    🔥