if Statement in Java
×


if Statement in Java

117

Introduction

The if statement in Java plays a vital role in decision-making by allowing a program to execute specific blocks of code based on certain conditions. It’s one of the most basic and widely used control flow statements in Java programming. In this blog, we’ll explore the syntax, logic, and use cases of the if statement with practical code examples.

What is an if Statement?

The if statement allows you to run a block of code only if a specified condition evaluates to true. If the condition is false, the code inside the if block is skipped entirely.

Syntax of if Statement


if (condition) {
    // code to execute if condition is true
}

The condition must result in a boolean value — either true or false. If it's true, the code block runs. Otherwise, it’s ignored.

Flow of Execution

  • The program evaluates the condition inside the if parentheses.
  • If the condition is true, it executes the statements inside the block.
  • If the condition is false, it skips the block and continues with the rest of the code.

Example: Simple if Statement


public class IfExample {
    public static void main(String[] args) {
        int number = 15;

        if (number > 10) {
            System.out.println("The number is greater than 10.");
        }

        System.out.println("Program continues...");
    }
}

Output:


The number is greater than 10.
Program continues...

Real-world Use Case

Suppose you're building a simple login system. You might use an if statement to check whether the entered password matches the expected one:


String enteredPassword = "admin123";
String correctPassword = "admin123";

if (enteredPassword.equals(correctPassword)) {
    System.out.println("Access Granted");
}

Important Notes

  • Don't put a semicolon right after the if condition — this can accidentally terminate the statement early.
  • Curly braces {} are optional for a single statement, but it’s recommended to always use them for clarity.

// This is legal but not recommended:
if (score > 50)
    System.out.println("Passed");

// Better with braces:
if (score > 50) {
    System.out.println("Passed");
}

Common Mistake: Using = Instead of ==

New Java programmers often confuse = (assignment) with == (comparison). Remember:


if (x = 5) {  // Incorrect: assignment, not comparison
    // This will result in a compilation error
}

if (x == 5) {  // Correct
    // This checks whether x is equal to 5
}

Conclusion

The if statement in Java is a foundational concept that every Java developer should understand well. It gives your program the ability to make logical decisions and control the flow of execution. By mastering its syntax and avoiding common mistakes, you'll be better equipped to build dynamic and responsive applications.



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