- Java Tutorial
- Java Introduction
- Java Features
- Java Simple Program
- JVM, JDK and JRE
- Java Syntax
- Java Comments
- Java Keywords
- Java Variables
- Java Literals
- Java Separators
- Java Datatypes
- Java Operators
- Java Statements
- Java Strings
- Java Arrays
- Control Statement
- Java If
- Java If-else
- Java If-else-if
- Java Nested If
- Java Switch
- Iteration Statement
- Java For Loop
- Java For Each Loop
- Java While Loop
- Java Do While Loop
- Java Nested Loop
- Java Break/Continue
- Java Methods
- Java Methods
- Java Method Parameters
- Java Method Overloading
- Java Recursion
- Java OOPS
- Java OOPs
- Java Classes/Objects
- Java Inheritance
- Java Polymorphism
- Java Encapsulation
- Java Abstraction
- Java Modifiers
- Java Constructors
- Java Interface
- Java static keyword
- Java this keyword
- Java File Handling
- Java File
- Java Create File
- Java Read/Write File
- Java Delete File
- Java Program To
- Add Two Numbers
- Even or Odd Numbers
- Reverse a String
- Swap Two Numbers
- Prime Number
- Fibonacci Sequence
- Palindrome Strings
- Java Reference
- Java String Methods
- Java Math Methods
Java Nested If Statement
In Java, decision-making is rarely a simple "yes or no" choice. Often, you need to check a condition only after another condition has already been met. This is where a nested if statement comes in. Essentially, it is an if block placed inside the body of another if block.
Think of it like a security clearance process: first, you check if the person has a badge (outer condition). If they do, you then check if that badge has access to a specific room (inner condition).
Syntax:
if (condition1) {
// This code runs if condition1 is true
if (condition2) {
// This code only runs if BOTH condition1 AND condition2 are true
}
}
Nested If Statement Logic:
- Hierarchy: The inner
ifstatement is entirely dependent on the outer one. If the first condition isfalse, the Java Virtual Machine (JVM) skips the entire block, and the inner condition is never even evaluated. - Scope: You can declare variables in the outer
ifblock that are then accessible to the innerifblock. - Refinement: Use nesting when you need to perform "layered" validation, such as checking if a user is logged in before checking if they have "Admin" privileges.
Example: Simple Range Check
int x = 10;
if (x > 5) {
// The program enters here because 10 is greater than 5
if (x < 15) {
// This inner check confirms x is also within the upper bound
System.out.println("x is between 5 and 15");
}
}
{}. While Java allows single-line if statements without braces, it makes nested logic extremely confusing and prone to bugs when you add more lines later.
Real-World Example: User Authentication
Consider a scenario where you are building a login system. You first check if the account exists, and only then do you check if the account is active.
boolean accountExists = true;
boolean isAccountActive = false;
if (accountExists) {
System.out.println("Checking account status...");
if (isAccountActive) {
System.out.println("Welcome back! Loading your dashboard.");
} else {
System.out.println("Error: Your account is suspended.");
}
} else {
System.out.println("Error: User not found.");
}
if doesn't require extra logic between the two conditions, consider using the Logical AND (&&) operator instead. It keeps your code "flatter" and easier to scan.
Summary
Nested if statements in Java provide a way to handle complex, multi-layered logic. By placing one condition inside another, you create a workflow where the inner logic only triggers if the outer requirements are met. This is fundamental for tasks like form validation, permission handling, and complex mathematical filtering in your Java applications.