Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🟡 Control Flow  ·  Lesson 10

if-else Statement in Java

In this lesson: Decision making in Java with if, if-else, else-if ladder and nested if — programs with output. In Hindi & English.

Making decisions

if-else statements let your program make decisions — do one thing if a condition is true, another if it is false. This is how programs react differently to different inputs, like showing "Pass" or "Fail" based on marks.

💡 if runs when the condition is true; else runs when it is false.

The if statement

int marks = 75;
if (marks >= 40) {
    System.out.println("Pass");
}
Pass

The if block runs only when the condition in brackets is true. If marks were below 40, nothing would print here.

if-else

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

With else, one of the two blocks always runs. If the condition is true, the if block runs; otherwise the else block does.

The else-if ladder

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

An else-if ladder checks conditions in order and runs the first true one. Here 82 is not >= 90 but is >= 75, so it prints Grade B and skips the rest.

Nested if

int age = 20;
boolean hasId = true;
if (age >= 18) {
    if (hasId) {
        System.out.println("Allowed");
    }
}
Allowed

An if can contain another if. Both conditions must be true here. For simple cases, you could also combine them with &&.

The ternary operator

int a = 10, b = 20;
int max = (a > b) ? a : b;
System.out.println("Max: " + max);
Max: 20

The ternary condition ? x : y is a compact if-else that returns a value. If the condition is true it gives x, else y — handy for short choices.

Common mistakes

  • Using = instead of ==. Conditions compare with ==, not assign with =.
  • Missing braces. Without { }, only the next line belongs to the if.
  • Wrong ladder order. Put stricter conditions first, or a looser one catches everything.
  • Semicolon after if. if (x); makes the if do nothing — do not add ;.

Summary

  • if runs a block when a condition is true; else when false.
  • An else-if ladder picks the first true condition among many.
  • Nested if checks a condition inside another.
  • The ternary cond ? x : y is a one-line if-else that returns a value.
  • Use == to compare and always prefer curly braces.

Next, handle many cases with switch-case, or repeat actions with Java loops.

Frequently Asked Questions

What is an if-else statement in Java?
An if-else statement lets a program make a decision. The if block runs when a condition is true, and the else block runs when it is false. It is how programs choose between two paths.
What is an else-if ladder?
An else-if ladder chains several conditions: if ... else if ... else if ... else. Java checks each in order and runs the first one that is true. It is used when there are more than two possible outcomes, like grading.
What is the ternary operator in Java?
The ternary operator is a short if-else that returns a value: condition ? valueIfTrue : valueIfFalse. For example int max = a > b ? a : b;. It is handy for simple choices in one line.
Why must if conditions use == and not =?
A single = assigns a value, while == compares. An if needs a true/false test, so it uses ==. Using = in a condition is a common bug (and often a compile error in Java).
Do I always need curly braces with if?
For a single statement, braces are optional, but always using { } is safer and clearer. Without braces, only the next single line belongs to the if, which can cause subtle bugs.
← Back to Java Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicSwitch Case Your progress: 10 / 61 (16%) · Lesson 11 / 61 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free