Java if-else statement;-
It is used to check the condition is true or false.
Various types of if-else statement:-
- if statement.
- if-else statement.
- if-else-if ladder.
- nested if statement.
If statement;-
It executes the if block if condition is true otherwise not executed.
Syntax of the if statement;-
If(test the expression)
{
// statements to be executed if the test expression is true.
}
Example-
class Student
{
public static void main(String[] args)
{
int age=25; if(age>18) { System.out.println(“Eligible for Vote”); } } } Output- Eligible for Vote.
If-else statement;-
It executes if part if the condition is true otherwise they execute else part.
Syntax-
if (test expression)
{
// statements to be executed if the test expression is true
}
else
{
// statements to be executed if the test expression is false
}
Example-
class Student
{
public static void main(String args[])
{
int a=10;
if(a%2==0)
{
System.out.println(“number is even”);
}
else
{
System.out.println(“number is odd”);
}
}
}
Output-
number is even.
if-else-if ladder;-
The if...else ladder allows you to check between multiple test expressions and execute different statements.
Syntax-
if (test expression1)
{
// statement(s)
}
else if(test expression2)
{
// statement(s)
}
else if (test expression3)
{
// statement(s)
}
.
.
else
{
// statement(s)
}
Example-
class Student
{
public static void main(String args[])
{
int age=38;
if(age>90)
{
System.out.println("dead");
}
else if(age>=0 && age<12)
{
System.out.println("child");
}
else if(age>=13 && age<22)
{
System.out.println("teenager");
}
else if(age>=23 && age<50)
{
System.out.println("younger");
}
else if(age>=51&& age<89)
{
System.out.println("older");
}
}
}
Output-
Younger.
nested if statement-;
The nested if statement represents the if block within another if block. Here, the inner if block condition executes only when outer if block condition is true.
Syntax:
if(condition){
//code to be executed
if(condition){
//code to be executed
}
}
Example-
class Student
{
public static void main(String args[])
{
int age=30;
int weight=70;
if(age>=18)
{
if(weight>55)
{
System.out.println("You are eligible to donate blood");
} }
}
}
Output-
You are eligible to donate blood.
Khushi Singh
12-Mar-2025The statement if-else in Java functions as a decision tool through block code execution that depends on specific criteria. The if statement runs a boolean condition check to determine if the block code of the if section needs execution. When the condition proves false then the else statement block (if specified) activates.
An if-else statement in Java begins with the if keyword which contains a parenthesized condition. When a condition meets the truth condition the inner if block gets executed. By default the program runs a separate else block block when any condition proves false.
Multiple conditions in Java can be checked by programming with if-else if-else statements. The else if statement serves sequential condition checking when multiple conditions need evaluation. During evaluation the process follows a sequential order by checking every condition until it finds the first match after which all remaining instructions become irrelevant. When all conditions fail to meet the requirements then the code in the final else block will run if it exists.
Java implements_nested if-else logic that enables any if or else block to include additional if statements. Using this structure allows users to make intricate decisions which depend on various connected conditions.
Java offers the ternary operator (? :) as an alternative to multiple if-else if-else statements to condense simple decision-making expressions within Java programs.
Explore more about if else statement here