Conditional Statements in Java :
Conditional statements help a Java program make decisions based on certain conditions. These
statements check whether a condition is true or false and execute different blocks of code
accordingly.
Types of Conditional Statements in Java
1. if statement
2. if-else statement
3. if-else-if ladder
4. nested if statement
5. switch statement
1. if Statement
The if statement checks a condition. If the condition is true, the block of code inside if runs.
Otherwise, it is skipped.
Syntax:
if (condition) {
// Code to execute if condition is true
Example:
public class IfExample {
public static void main(String[] args) {
int number = 10;
if (number > 0) { // Condition is true
System.out.println("The number is positive.");
System.out.println("This statement is always executed.");
Output:
The number is positive.
This statement is always executed.
2. if-else Statement
The if-else statement is used when we want to execute one block of code if a condition is true and
another block if the condition is false.
Syntax:
if (condition) {
// Code to execute if condition is true
} else {
// Code to execute if condition is false
Example:
public class IfElseExample {
public static void main(String[] args) {
int number = -5;
if (number > 0) {
System.out.println("The number is positive.");
} else {
System.out.println("The number is not positive.");
Output:
The number is not positive.
3. if-else-if Ladder
The if-else-if ladder is useful when we have multiple conditions to check. The program checks each
condition one by one, and once a condition is found true, the corresponding block is executed.
Syntax:
if (condition1) {
// Code if condition1 is true
} else if (condition2) {
// Code if condition2 is true
} else if (condition3) {
// Code if condition3 is true
} else {
// Code if all conditions are false
Example:
public class IfElseIfExample {
public static void main(String[] args) {
int marks = 85;
if (marks >= 90) {
System.out.println("Grade: A");
} else if (marks >= 80) {
System.out.println("Grade: B");
} else if (marks >= 70) {
System.out.println("Grade: C");
} else if (marks >= 60) {
System.out.println("Grade: D");
} else {
System.out.println("Grade: F (Fail)");
}
Output:
Grade: B
4. Nested if Statement
A nested if statement means an if statement inside another if. It is used when we need to check
multiple conditions, one inside another.
Syntax:
if (condition1) {
if (condition2) {
// Code if both condition1 and condition2 are true
Example:
public class NestedIfExample {
public static void main(String[] args) {
int age = 20;
int weight = 55;
if (age >= 18) { // First condition
if (weight >= 50) { // Second condition
System.out.println("You are eligible to donate blood.");
} else {
System.out.println("You need to weigh at least 50 kg.");
} else {
System.out.println("You must be 18 or older to donate blood.");
Output:
You are eligible to donate blood.
5. switch Statement
The switch statement is used when we want to test a variable against multiple values. It is an
alternative to if-else-if when we have many conditions.
Syntax:
switch (variable) {
case value1:
// Code to execute if variable == value1
break;
case value2:
// Code to execute if variable == value2
break;
case value3:
// Code to execute if variable == value3
break;
default:
// Code to execute if none of the cases match
Example:
public class SwitchExample {
public static void main(String[] args) {
int day = 3;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
default:
System.out.println("Invalid day number");
Output:
Wednesday
Key Differences Between if-else and switch
Feature if-else switch
Used for Checking complex conditions Checking a single variable against multiple values
Data types Works with all types (boolean, int, float, etc.) Works with int, char, String, enum
Execution Checks all conditions even if the first one is true Jumps directly to the matching case
Performance Slower when many conditions Faster for large cases due to direct jump
Conclusion
Use if when you have one condition.
Use if-else when you need an alternative case.
Use if-else-if when checking multiple conditions.
Use nested if for conditions inside conditions.
Use switch when a single variable has many possible values.
Condition :1 Percentage greator than or equal to 90
Condition :2 Percentage greator to 85
Condition :3 Percentage less than or equal to 70
Condition :4 Percentage equal to 0