The if-else
statement in Java is a fundamental control flow construct that allows your program to make decisions based on whether a condition is true or false. It enables you to execute different blocks of code depending on the outcome of a boolean expression.
Basic Structure:
if (condition) {
// Code to execute if the condition is true
} else {
// Code to execute if the condition is false
}
Explanation:
-
if (condition)
: Theif
keyword is followed by a condition enclosed in parentheses. This condition must be a boolean expression, meaning it evaluates to eithertrue
orfalse
. -
{ ... }
(if block): The code within the curly braces immediately following theif
condition is called the “if block.” This block of code is executed only if the condition evaluates totrue
. -
else { ... }
(else block): Theelse
keyword is optional. If present, it’s followed by another block of code enclosed in curly braces, called the “else block.” This block of code is executed only if the condition evaluates tofalse
.
How it Works:
The if-else
statement works as follows:
- The condition is evaluated.
- If the condition is
true
, the code in theif
block is executed, and the code in theelse
block (if present) is skipped. - If the condition is
false
, the code in theif
block is skipped, and the code in theelse
block (if present) is executed.
Examples:
1. Simple if
statement:
int age = 16;
if (age >= 18) {
System.out.println("You are an adult.");
}
In this example, the message “You are an adult” will only be printed if the value of the age
variable is greater than or equal to 18. If the age is less than 18, nothing will be printed.
2. if-else
statement:
int temperature = 25;
if (temperature > 30) {
System.out.println("It's hot!");
} else {
System.out.println("It's a pleasant temperature.");
}
Here, if the temperature is greater than 30, “It’s hot!” is printed. Otherwise (if the temperature is 30 or less), “It’s a pleasant temperature.” is printed.
3. Nested if-else
statements:
You can nest if-else
statements within each other to create more complex decision-making logic:
int score = 85;
if (score >= 70) {
System.out.println("You passed.");
if (score >= 90) {
System.out.println("Excellent!");
} else if (score >= 80) {
System.out.println("Good job!");
}
} else {
System.out.println("You did not pass.");
}
In this nested example, the outer if
checks if the score is passing (70 or above). If it is, then the inner if-else if
checks for specific grade ranges (80-89 and 90+).
4. Using boolean variables:
You can directly use boolean variables in the condition:
boolean isLoggedIn = true;
if (isLoggedIn) {
System.out.println("Welcome!");
} else {
System.out.println("Please log in.");
}
Important Considerations:
-
Curly Braces: While curly braces are technically optional for single-line
if
andelse
blocks, it’s highly recommended to always use them. This improves code readability and prevents errors, especially when adding more statements later. -
Conditions: The condition in the
if
statement must be a boolean expression. You can use comparison operators (e.g.,==
,!=
,>
,<
,>=
,<=
) and logical operators (e.g.,&&
(AND),||
(OR),!
(NOT)) to create complex conditions. -
else if
: For checking multiple conditions in sequence, use theelse if
construct (as shown in the nested example) to avoid deeply nestedif
statements.