Java Language – 6 – Conditional Statements

Java Fundamentals – Conditional Statements
Introduction

Conditional statements in Java allow you to make decisions and execute different blocks of code based on specific conditions. These constructs are vital for controlling the flow of your Java programs and adding logic to your applications. In this guide, we’ll explore the key conditional statements in Java.

‘if’ Statement

The ‘if’ statement is one of the fundamental conditional constructs in Java. It allows you to execute a block of code if a specified condition is true. Here’s an example:


int age = 25;

if (age >= 18) {
    System.out.println("You are an adult.");
}

In this example, the code inside the ‘if’ block will be executed because the condition (age is greater than or equal to 18) is true.

‘if-else’ Statement

The ‘if-else’ statement is an extension of the ‘if’ statement, allowing you to execute different code blocks based on whether a condition is true or false. Here’s how it works:


int age = 15;

if (age >= 18) {
    System.out.println("You are an adult.");
} else {
    System.out.println("You are not yet an adult.");
}

In this case, the code inside the ‘if’ block is executed if the condition is true, while the ‘else’ block is executed if the condition is false.

‘if-else if-else’ Statement

The ‘if-else if-else’ statement is used when you have multiple conditions to evaluate, and you want to execute the first block where the condition is true. It is structured as follows:


int score = 75;

if (score >= 90) {
    System.out.println("Excellent!");
} else if (score >= 70) {
    System.out.println("Good job.");
} else {
    System.out.println("Keep working hard.");
}

In this example, the first ‘if’ condition is checked, and if it’s true, the corresponding block is executed. If not, the ‘else if’ conditions are checked in sequence.

‘switch’ Statement

The ‘switch’ statement is used when you have multiple options and you want to execute code based on the value of a variable or expression. It’s an efficient way to simplify complex branching logic:


int day = 3;
String dayName;

switch (day) {
    case 1:
        dayName = "Monday";
        break;
    case 2:
        dayName = "Tuesday";
        break;
    default:
        dayName = "Unknown";
        break;
}

System.out.println("Today is " + dayName);

The ‘switch’ statement evaluates the value of ‘day’ and executes the code block associated with the matching ‘case.’

Nested Conditional Statements

Java allows you to nest conditional statements within each other to create complex logic. This is especially useful when you need to evaluate multiple conditions in a hierarchical manner:


int x = 10;
int y = 5;

if (x > 5) {
    if (y < 8) {
        System.out.println("Both conditions are true.");
    } else {
        System.out.println("First condition is true, but not the second.");
    }
} else {
    System.out.println("First condition is false.");
}

In this nested example, you first check if ‘x’ is greater than 5. If it is, you then check if ‘y’ is less than 8, and so on.

Conclusion

Conditional statements are powerful tools in Java programming for making decisions, handling different scenarios, and creating responsive applications. By mastering these constructs, you gain the ability to control the flow of your programs and add dynamic behavior to your code.