Skip to content

If-else statements

In this guide we're diving deep into the world of Java if-else statements. If you've ever wondered how to make your code smarter and more responsive to different situations, you're in the right place! So, let's jump in and explore the wonders of if-else statements together.

Imagine you're writing a program and you want it to behave differently based on certain conditions. That's where if-else statements come into play! They're like the decision-makers in your code, allowing you to choose one path over another depending on specific criteria. It's like having a superpower that lets your code adapt and respond to different scenarios.

Types of If-Else Statements

Simple If Statement

The simple if statement is the most basic form of decision-making in Java. It evaluates a condition and executes a block of code if the condition is true.

java
if (condition) {
    // Code to be executed if condition is true
}

Example:

java
int num = 10;
if (num > 0) {
    System.out.println("The number is positive.");
}
mermaid
graph TD;
    A[Condition] -->|true| B[Execute code];

If-Else Statement

The if-else statement extends the simple if statement by providing an alternative block of code to execute if the condition is false.

java
if (condition) {
    // Code to be executed if condition is true
} else {
    // Code to be executed if condition is false
}

Example:

java
int num = -5;
if (num > 0) {
    System.out.println("The number is positive.");
} else {
    System.out.println("The number is negative or zero.");
}
mermaid
graph TD;
    A[Condition] -->|true| B[Execute code];
    A -->|false| C[Execute alternative code];

Nested If Statement

A nested if statement is an if statement inside another if statement. It allows for multiple levels of decision-making based on various conditions.

java
if (condition1) {
    if (condition2) {
        // Code to be executed if both conditions are true
    }
}

Example:

java
int num = 10;
if (num > 0) {
    if (num % 2 == 0) {
        System.out.println("The number is positive and even.");
    }
}
mermaid
graph TD;
    A[Outer Condition] -->|true| B[Inner Condition];
    B -->|true| C[Execute code];

Else-If Statement

The else-if statement allows you to evaluate multiple conditions sequentially. If the first condition is false, it moves on to the next condition and so on.

java
if (condition1) {
    // Code to be executed if condition1 is true
} else if (condition2) {
    // Code to be executed if condition2 is true
} else {
    // Code to be executed if all conditions are false
}

Example:

java
int num = 0;
if (num > 0) {
    System.out.println("The number is positive.");
} else if (num < 0) {
    System.out.println("The number is negative.");
} else {
    System.out.println("The number is zero.");
}
mermaid
graph TD;
    A[Condition1] -->|true| B[Execute code];
    A -->|false| C[Check next condition];
    C -->|true| D[Execute alternative code];
    C -->|false| E[Execute final code];

Java if-else statements are like the traffic signals of your code, directing its flow and making smart decisions along the way. Whether you need to handle different cases, validate user input, or respond to changing conditions, if-else statements are your go-to tool for creating dynamic and responsive programs.

Waytojava is designed to make learning easier. We simplify examples for better understanding. We regularly check tutorials, references, and examples to correct errors, but it's important to remember that humans can make mistakes.