Yandex

Java Advanced ConceptsJava Advanced Concepts3

Java ReferenceJava Reference1

Java If Statement
Syntax and Examples



The if statement in Java is like a gate. It opens only if a certain condition is true. Otherwise, it quietly ignores the code block inside it. This simple control structure forms the basis for decision-making in Java programs.

Syntax of If Statement in Java

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

The condition must evaluate to a sboolean value, either true or false. If it's true, the code inside the block executes. If not, the program simply skips that block and moves on.

Example 1: Basic Java If Statement

public class IfExample1 {
    public static void main(String[] args) {
        int number = 10;
        if (number > 0) {
            System.out.println("The number is positive.");
        }
        System.out.println("Program ended.");
    }
}
The number is positive.
Program ended.

Explanation:

Since number > 0 evaluates to true, the message inside the if block is printed. The last line runs regardless of the condition.

Step-by-Step Execution Flow:

  1. Variable number is assigned 10.
  2. The condition number > 0 becomes true.
  3. The message is printed.
  4. The program continues with the next statement after the if block.

Example 2: Condition That Fails

public class IfExample2 {
    public static void main(String[] args) {
        int age = 15;
        if (age >= 18) {
            System.out.println("You can vote.");
        }
        System.out.println("Check completed.");
    }
}
Check completed.

Explanation:

Here, the condition age >= 18 is false. So, the code inside the if block is ignored. Only the final line runs.

Best Practices When Using If Statements

Common Mistakes to Avoid

Example 3: No Curly Braces – What Happens?

public class IfExample3 {
    public static void main(String[] args) {
        int marks = 85;
        if (marks > 50)
            System.out.println("You passed!");
            System.out.println("This message is outside the if.");
    }
}
You passed!
This message is outside the if.

Explanation:

Only the first print statement is inside the if. The second line executes every time. To avoid such surprises, always use curly braces.

Summary

The Java if statement allows your programs to make decisions. Always test your conditions, keep your code readable, and avoid syntax pitfalls. As you progress, if will serve as a foundation for more advanced constructs like else, else if, and switch statements.

QUIZ

Question 1:What does the following code output?
int num = 5;
if (num < 10) {
    System.out.println("Less than 10");
}
System.out.println("Done");

Question 2:In Java, the condition inside an if statement must always evaluate to a boolean value.

Question 3:Which of the following are best practices when using if statements in Java?

Question 4:What happens when the following code is executed?
int age = 15;
if (age >= 18) {
    System.out.println("You can vote.");
}
System.out.println("Check completed.");

Question 5:The following code produces a logical error due to a misplaced semicolon:
int x = 10;
if (x > 5);
{
    System.out.println("Greater than 5");
}

Question 6:Review the code below. Which statements about it are true?
int marks = 85;
if (marks > 50)
    System.out.println("You passed!");
    System.out.println("This message is outside the if.");



Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You can support this website with a contribution of your choice.

When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M