- 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 If-else Statement
In Java, the if-else statement is the most fundamental way to control the flow of your program. By default, Java code executes line-by-line from top to bottom. However, in the real world, programs need to make decisions like checking if a user is logged in, if a sensor reading is too high, or if a player has enough points to level up. The if-else statement allows your code to branch into different paths based on specific conditions.
Syntax:
if (condition) {
// Code to execute if the condition is true
} else {
// Code to execute if the condition is false
}
The "condition" inside the parentheses must always evaluate to a boolean value (either true or false). If the condition is true, the code block inside the if braces runs. If it is false, the else block runs instead.
= instead of a double equals ==. In Java, = is for assignment (setting a value), while == is for comparison (checking if values are equal).
Simple If-else Statement:
- Executes one block of code if the condition is true, and another block if the condition is false.
- Example: A simple check to see if a user has passed a threshold.
int x = 10;
if (x > 5) {
System.out.println("x is greater than 5");
} else {
System.out.println("x is not greater than 5");
}
{} even for single-line statements. While Java allows you to skip them for one line of code, it often leads to bugs during future code updates when a second line is added but not properly wrapped in the block.
Chained If-else Statement:
- Allows multiple conditions to be checked sequentially. This is often called an "else-if ladder."
- Once Java finds a condition that is true, it executes that block and skips the rest of the chain.
- Example: Categorizing a person based on their age.
int age = 20;
if (age < 18) {
System.out.println("You are a minor");
} else if (age >= 18 && age < 65) {
System.out.println("You are an adult");
} else {
System.out.println("You are a senior citizen");
}
age > 0) at the top, it might trigger before your more specific conditions further down the chain.
Nested If-else Statement:
- An if-else statement inside another if-else statement. This is useful when one decision depends on the outcome of a previous one.
- Example: Validating a number first to see if it is non-negative, and then checking if it is specifically zero.
int num = -5;
if (num >= 0) {
if (num == 0) {
System.out.println("Number is zero");
} else {
System.out.println("Number is positive");
}
} else {
System.out.println("Number is negative");
}
Summary
The if-else statement in Java provides a way to execute different blocks of code based on whether a condition is true or false. It allows for decision-making in Java programs, enabling the creation of more dynamic and responsive applications. From simple binary choices to complex multi-step validation logic, understanding how to structure if-else statements is essential for controlling program flow and implementing reliable logic in any Java application.
.equals() instead of ==. For example: if (username.equals("admin")). Using == with Strings checks if they are the same object in memory, not if they contain the same characters.