Introduction
The Java if statement is one of the most important decision-making statements in Java programming. It allows a program to execute a block of code only when a specified condition is true. Without conditional statements, every line of code would execute sequentially, making it impossible to build dynamic applications.
For example:
- If the user’s password is correct, allow login.
- If a student scores above 40 marks, display “Pass.”
In this tutorial, you’ll learn everything about the Java if statement, including its syntax, examples, real-world applications, common mistakes, interview questions, and best practices.
What is Java if Statement?
The Java if statement is a conditional statement used to execute a block of code only if a specified condition evaluates to true.
If the condition is false, Java skips the code inside the if block and continues executing the remaining program.
It follows this basic logic:
If the condition is true → Execute the code.
If the condition is false → Skip the code.
The condition must always evaluate to a boolean value (true or false).
Example
int age = 20;
if (age >= 18) {
System.out.println("Eligible to vote");
}
Output
Since the condition age >= 18 is true, Java executes the statement.
Syntax
The syntax of the Java if statement is straightforward.
if (condition) {
// code to execute
}
Explanation
- if is a Java keyword.
- condition must return true or false.
- Curly braces {} contain the statements to execute.
- If the condition is false, Java skips the block.
Flow of Java if Statement
The execution process is:
- Java evaluates the condition.
- If the condition is true, execute the block.
- If the condition is false, skip the block.
- Continue with the next statement.
Flow:
Condition
|
True --------> Execute Block
|
False -------> Skip Block
Example 1: Simple if Statement
public class Main {
public static void main(String[] args) {
int marks = 75;
if (marks >= 40) {
System.out.println("You Passed");
}
System.out.println("Exam Completed");
}
}
Output
Exam Completed
Since the condition is true, the message is displayed.
Example 2: Condition is False
public class Main {
public static void main(String[] args) {
int age = 15;
if (age >= 18) {
System.out.println("Eligible for Driving License");
}
System.out.println("Program Finished");
}
}
Output
The condition is false, so Java skips the if block.
Example 3: Using Boolean Variable
public class Main {
public static void main(String[] args) {
boolean isJavaFun = true;
if (isJavaFun) {
System.out.println("Let's Learn Java!");
}
}
}
Output
Example 4: Multiple Statements
int salary = 50000;
if (salary > 30000) {
System.out.println("High Salary");
System.out.println("Eligible for Premium Card");
System.out.println("Congratulations!");
}
Output
Eligible for Premium Card
Congratulations!
Operators Commonly Used with if Statement
The if statement is usually combined with comparison and logical operators.
| Operator | Description | Example |
|---|---|---|
| == | Equal to | age == 18 |
| != | Not equal | age != 18 |
| > | Greater than | marks > 50 |
| < | Less than | marks < 40 |
| >= | Greater than or equal | salary >= 30000 |
| <= | Less than or equal | price <= 100 |
| && | VLogical AND | age > 18 && citizen |
| || | Logical OR | marks > 90 || sportsQuota |
| ! | Logical NOT | !isLoggedIn |
Real-life Example
Imagine you’re creating an ATM application.
The user wants to withdraw money.
int balance = 8000;
int withdraw = 5000;
if (balance >= withdraw) {
balance = balance - withdraw;
System.out.println("Transaction Successful");
System.out.println("Remaining Balance: " + balance);
}
Output
Remaining Balance: 3000
The withdrawal happens only if sufficient balance is available.
Student Result System
int marks = 82;
if (marks >= 40) {
System.out.println("Student Passed");
}
Login Verification
boolean loggedIn = true;
if (loggedIn) {
System.out.println("Welcome User");
}
Online Shopping
double totalAmount = 6000;
if (totalAmount >= 5000) {
System.out.println("Free Delivery Available");
}
Temperature Alert
int temperature = 39;
if (temperature > 35) {
System.out.println("Heat Wave Warning");
}
Advantages of Java if Statement
- Makes programs intelligent.
- Executes code only when required.
- Improves efficiency.
- Easy to understand and use.
- Forms the basis for decision-making in Java.
- Essential for real-world applications.
Common Mistakes
1. Using Assignment Instead of Comparison
Incorrect:
if (age = 18)
Correct:
if (age == 18)
2. Missing Curly Braces
Incorrect:
if (marks > 40)
System.out.println("Pass");
System.out.println("Congratulations");
Only the first statement belongs to the if block.
Correct:
if (marks > 40) {
System.out.println("Pass");
System.out.println("Congratulations");
}
3. Forgetting Parentheses
Incorrect:
Incorrect:f age > 18 {
}
Correct:
if (age > 18) {
}
4. Using Non-Boolean Conditions
Incorrect:
int age = 20;
if (age) {
}
Java does not allow integers as conditions.
Correct:
if (age > 18) {
}
5. Adding an Extra Semicolon
Incorrect:
if (marks > 40);
{
System.out.println("Pass");
}
The semicolon ends the if statement, causing the block to execute regardless of the condition.
Correct:
if (marks > 40) {
System.out.println("Pass");
}
Best Practices
- Always use meaningful variable names.
- Keep conditions simple and readable.
- Use braces even for single-line if blocks.
- Avoid deeply nested if statements when possible.
- Test both true and false conditions during development.
- Combine conditions carefully using logical operators.
Conclusion
The Java if statement is one of the fundamental building blocks of programming and an essential part of writing intelligent Java applications. It allows your program to make decisions by executing code only when specific conditions are met.
In this guide, you learned what the Java if statement is, its syntax, execution flow, multiple practical examples, real-life use cases, common mistakes to avoid, best practices, and frequently asked interview questions.