if-else Statement in Java
×


if-else Statement in Java

123

Introduction

In Java, the if-else statement is one of the most commonly used control structures for decision-making. It allows a program to choose between two different paths of execution based on whether a condition evaluates to true or false. This is particularly useful when you want to take an action in one case and a different action otherwise.

What is the if-else Statement?

The if-else statement checks a condition. If the condition is true, the code inside the if block runs. If it’s false, the code inside the else block gets executed. This structure helps in branching logic and managing alternate paths in your code.

Syntax of if-else Statement


if (condition) {
    // executes when condition is true
} else {
    // executes when condition is false
}

Basic Example


public class IfElseExample {
    public static void main(String[] args) {
        int age = 20;

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

Output:


You are eligible to vote.

Real-life Use Case

Consider a temperature checker app. If the temperature is above a certain level, it shows a warning; otherwise, it displays that the temperature is normal. Here's how it might look:


int temperature = 36;

if (temperature > 37) {
    System.out.println("Warning: High body temperature!");
} else {
    System.out.println("Body temperature is normal.");
}

Key Points to Remember

  • Use the if block when the condition is expected to be true.
  • The else block runs only if the condition is false.
  • Only one of the two blocks is executed — never both.
  • Curly braces are optional for single-line statements, but recommended for clarity.

Common Mistake to Avoid

A frequent error is using a single = instead of == in the condition, which leads to an assignment rather than a comparison:


// Incorrect:
if (x = 10) {  // Assignment, not comparison
    System.out.println("Wrong logic!");
}

// Correct:
if (x == 10) {
    System.out.println("Correct logic.");
}

Nested if-else Example

Sometimes, decisions can be more complex and require another if-else inside an else or if block.


int score = 75;

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

When to Use if-else

  • When you have a binary decision to make — either one thing or the other.
  • When you want to execute different code based on different conditions.
  • For basic validations, access control, and conditional logic in applications.

Conclusion

The if-else statement in Java is a crucial tool for controlling the flow of your program. It helps the code respond to different situations dynamically by choosing between two logical branches. Once you're comfortable with if-else, you're well on your way to writing smarter and more adaptable Java programs.



If you’re passionate about building a successful blogging website, check out this helpful guide at Coding Tag – How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!

For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!



Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments

Coding Tag WhatsApp Chat