Conditional statement in java play a crucial role in Java programming by controlling the flow of execution based on certain conditions. You can make decisions and perform different actions based on the input data using these statements.
Table of Contents
Conditional Statements
The if Statement
The “if” statement is the simplest form of conditional statement in Java. It evaluates a given condition and, if true, executes the code inside the if block.
public class IfExample {
public static void main(String[] args) {
int score = 75;
if (score >= 60) {
System.out.println("Pass");
}
}
}
Output

The if-Else Statement
The “if-else” statement offers an alternate execution path when the if statement’s condition is false. It specifies an additional block of code to be executed if the “if” condition is false.
public class IfElseExample {
public static void main(String[] args) {
int score = 75;
if (score >= 60) {
System.out.println("Pass");
} else {
System.out.println("Fail");
}
}
}
Output

The If-Else-If Ladder
The “if-else-if ladder” checks multiple conditions. It is a series of “if-else” statements, each specifying a different action based on a different condition. The first “if” statement that evaluates to true will execute its code block, ignoring the rest of the conditions.
public class IfElseIfExample {
public static void main(String[] args) {
int score = 75;
if (score >= 90) {
System.out.println("A");
} else if (score >= 80) {
System.out.println("B");
} else if (score >= 70) {
System.out.println("C");
} else if (score >= 60) {
System.out.println("D");
} else {
System.out.println("F");
}
}
}
Output

The Nested-if Statement
Conditional statements provide the necessary control flow mechanism in Java programming. By making decisions based on input data, you can write complex programs with ease.
public class Main {
public static void main(String[] args) {
int x = 30;
int y = 10;
if (x == 30) {
if (y == 10) {
System.out.print("X = 30 and Y = 10");
}
}
}
}
Output

Conclusion
Conditional statement in java provide the necessary control flow mechanism in Java programming. They enable you to make decisions based on input data, which is crucial in writing complex programs.