🟢 if Statements in Java
Every program makes decisions. Should it print a message, allow login, or show an error? 🤔
That’s where the if statement comes in. It gives your Java programs the power to make choices and control the flow of execution.
Think of it as the brain of your code 🧠 — deciding what to do based on conditions.
🧐 What is an if statement?
An if statement lets you run a block of code only if a condition is true.
👉 Syntax:
if(condition) {
// Code executes if condition is true
}
🔍 How it Works
1️⃣ Condition is checked.
2️⃣ If true → block executes.
3️⃣ If false → block is skipped.
✅ Example 1: Simple if
int age = 20;
if(age >= 18) {
System.out.println("You are eligible to vote!");
}
Output:
You are eligible to vote!
🚀 Why use if statement?
✔️ Adds decision-making power to programs.
✔️ Used in validations, conditions, error handling.
✔️ Makes code more dynamic & interactive.
⚡ Types of if Statements in Java
-
if -
if-else -
if-else-if ladder -
nested if
📝 Syntax and Purpose of if Statements in Java
| Statement | Syntax | Purpose |
|---|---|---|
| if | java<br>if(condition) {<br> // code executes if condition is true<br>}<br> |
Executes only if condition is true |
| if-else | java<br>if(condition) {<br> // code if condition is true<br>} else {<br> // code if condition is false<br>}<br> |
Chooses between two blocks |
| if-else-if | java<br>if(condition1) {<br> // code if condition1 is true<br>} else if(condition2) {<br> // code if condition2 is true<br>} else {<br> // code if none are true<br>}<br> |
Handles multiple conditions |
| nested if | java<br>if(condition1) {<br> if(condition2) {<br> // code if both are true<br> }<br>}<br> |
Decision inside a decision |
1️⃣ if statement
Executes code only if condition is true.
if(score > 50) {
System.out.println("You passed!");
}
2️⃣ if-else statement
Executes one block if condition is true, otherwise another.
if(score >= 50) {
System.out.println("Pass");
} else {
System.out.println("Fail");
}
3️⃣ if-else-if ladder
Used for multiple conditions.
if(marks >= 90) {
System.out.println("Grade A");
} else if(marks >= 75) {
System.out.println("Grade B");
} else if(marks >= 50) {
System.out.println("Grade C");
} else {
System.out.println("Fail");
}
4️⃣ Nested if statement
if inside another if.
int age = 25;
boolean hasID = true;
if(age >= 18) {
if(hasID) {
System.out.println("Entry allowed!");
}
}
🎯 Real-Life Use Cases of if
✔️ Login authentication (check username & password)
✔️ E-commerce discounts (apply offers if eligible)
✔️ Banking apps (allow withdrawal only if balance is sufficient)
✔️ Games (check if player’s health > 0)
💡 Pro Tips
✅ Always use logical operators (&&, ||) for complex conditions.
✅ Keep conditions simple & readable.
✅ Use braces {} for clarity, even for one-line code.
✅ Combine with loops for powerful decision-making.
🔑 Quick Comparison
| Statement | Purpose |
|---|---|
| if | Executes only if condition is true |
| if-else | Chooses between two blocks |
| if-else-if | Handles multiple conditions |
| nested if | Decision inside a decision |
✅ In short:
The if statement is the foundation of decision-making in Java. Once you master it, you unlock the power to write smart, interactive, and real-world applications. 🚀
🌟 Popular Examples of if Statements in Java
1️⃣ Check Voting Eligibility
int age = 20;
if(age >= 18) {
System.out.println("✅ You are eligible to vote!");
}
Output:
✅ You are eligible to vote!
2️⃣ Odd or Even Number
int num = 7;
if(num % 2 == 0) {
System.out.println(num + " is Even");
} else {
System.out.println(num + " is Odd");
}
Output:
7 is Odd
3️⃣ Positive, Negative, or Zero
int number = -5;
if(number > 0) {
System.out.println("Positive");
} else if(number < 0) {
System.out.println("Negative");
} else {
System.out.println("Zero");
}
Output:
Negative
4️⃣ Find Largest of Three Numbers
int a = 25, b = 40, c = 30;
if(a >= b && a >= c) {
System.out.println(a + " is the largest");
} else if(b >= a && b >= c) {
System.out.println(b + " is the largest");
} else {
System.out.println(c + " is the largest");
}
Output:
40 is the largest
5️⃣ Login Authentication (Simple Example)
String username = "admin";
String password = "1234";
if(username.equals("admin") && password.equals("1234")) {
System.out.println("✅ Login Successful!");
} else {
System.out.println("❌ Invalid credentials!");
}
Output:
✅ Login Successful!
6️⃣ Student Grade Calculator
int marks = 82;
if(marks >= 90) {
System.out.println("Grade A");
} else if(marks >= 75) {
System.out.println("Grade B");
} else if(marks >= 50) {
System.out.println("Grade C");
} else {
System.out.println("Fail");
}
Output:
Grade B