Java Lesson 10 - Control Statements | Dataplexa

Control Statements in Java

So far, Java programs have executed instructions in a straight line, one statement after another. But real applications do not work that way.

Programs must make decisions, repeat actions, and choose different paths based on conditions. This is where control statements come into play.


What Are Control Statements?

Control statements allow Java programs to control the flow of execution. They decide which code runs, when it runs, and how many times it runs.

Using control statements, programs can respond to user input, validate data, and implement business rules.


Types of Control Statements in Java

Java control statements are broadly classified into three categories:

  • Decision-making statements
  • Looping statements
  • Jump statements

In this lesson, we focus on decision-making statements. Looping statements are covered in the next lesson.


The if Statement

The if statement allows Java to execute code only when a condition is true.

This is commonly used in situations like checking eligibility, validating input, or verifying conditions.

public class IfExample {

    public static void main(String[] args) {

        int age = 20;

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

Here, the message is printed only if the condition evaluates to true.


The if-else Statement

Sometimes, programs need to choose between two paths. The if-else statement helps handle such situations.

This is commonly used in pass/fail logic or access control.

public class IfElseExample {

    public static void main(String[] args) {

        int marks = 45;

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

The else-if Ladder

When there are multiple conditions to check, Java uses the else-if ladder.

This is useful in grading systems, pricing slabs, and classification logic.

public class ElseIfExample {

    public static void main(String[] args) {

        int score = 78;

        if (score >= 90) {
            System.out.println("Grade: A");
        } else if (score >= 75) {
            System.out.println("Grade: B");
        } else if (score >= 60) {
            System.out.println("Grade: C");
        } else {
            System.out.println("Grade: D");
        }
    }
}

Java checks conditions from top to bottom and executes the first true block.


The switch Statement

The switch statement is used when there are many fixed options. It is often cleaner than long if-else chains.

Examples include menu selection or option-based navigation.

public class SwitchExample {

    public static void main(String[] args) {

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

Real-World Use of Control Statements

Control statements are used everywhere in real applications:

  • User authentication and authorization
  • Transaction validation
  • Menu-driven programs
  • Decision-based workflows

Mastering control statements is essential before moving into loops and advanced logic.


What You Learned in This Lesson

  • What control statements are
  • How Java makes decisions using if and else
  • How to handle multiple conditions
  • When to use switch statements

In the next lesson, you will learn about loops, which allow Java programs to repeat actions efficiently.