Conditional Statement - if-else Statement

Published: (February 3, 2026 at 04:40 AM EST)
2 min read
Source: Dev.to

Source: Dev.to

Conditional Statement / Decision Making Statement / Control Flow Statement – if‑else Statement

  • The if‑else statement is used to make decisions in a program based on a boolean condition.
  • It executes the code block only if the condition is true.

Core Conditional Statements

if Statement

The most basic form. It executes a block of code only if the specified condition is true.

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

if‑else Statement

Provides an alternative path. If the if condition is false, the else block executes.

if (marks >= 35) {
    System.out.println("Pass");
} else {
    System.out.println("Fail");
}

else‑if Ladder

Used to check multiple conditions. Once a true condition is found, its block runs, and the rest of the ladder is skipped.

if (marks >= 90) {
    System.out.println("Grade A");
} else if (marks >= 75) {
    System.out.println("Grade B");
} else if (marks >= 35) {
    System.out.println("Grade C");
} else {
    System.out.println("Fail");
}

Core Rules

  • Boolean Condition Required: The condition within the if statement parentheses () must be a boolean expression. Non‑boolean values (e.g., integers) cannot be used as conditions in Java.
  • else is Optional: An if statement can exist alone without an else block.
  • Curly Braces {}: If the if or else block contains more than one statement, enclose them within curly braces {}.
  • Execution Flow: In an if‑else‑if ladder, conditions are evaluated from top to bottom. As soon as a condition evaluates to true, its corresponding block of code is executed, and the remaining conditions are skipped.
Back to Blog

Related posts

Read more »

Operators

What are Operators in Java? Operators are symbols used to perform operations on variables and values. Types of Operators in Java - Arithmetic Operators - Assig...

JDK

What is JDK? JDK is a complete software package used to develop Java applications. It contains tools to write, compile, debug, and run Java programs. When is J...