Conditional Statements in Java
Subject: Data Structures and Algorithms · Language: Java · Level: beginner · 6 min read
Learn if, if else, else if ladder, nested if, switch, and decision-making logic.
Conditional statements are used when a program needs to make a decision. In real life, we make decisions all the time: if marks are high, print a good grade; if a number is divisible by 2, call it even; if a password is correct, allow login. In Java, conditions are written using expressions that produce a boolean result: `true` or `false`. Based on that result, the program decides which block of code should run.
Conditional statements are very important in DSA because they help control logic. Searching, sorting, counting, validation, boundary checks, and decision-based problems all use conditions.
if Statement
`if` is used when we want to run a block of code only when a condition is true. If the condition is false, Java simply skips that block and continues with the next part of the program.
```java
if (marks >= 40) {
System.out.println("Pass");
}
```
Here, `marks >= 40` is the condition. If it is true, `Pass` is printed. If it is false, nothing inside the `if` block runs.
if else Statement
`if else` is used when there are exactly two possible paths. One path runs when the condition is true, and the other path runs when the condition is false.
```java
if (marks >= 40) {
System.out.println("Pass");
} else {
System.out.println("Fail");
}
```
This structure is useful when the program must choose one result from two options, such as pass/fail, even/odd, eligible/not eligible, or found/not found.
else if Ladder
`else if` is used when there are multiple conditions to check one by one. Java checks from top to bottom and runs the first block whose condition is true. A grading system is a common example because marks may fall into Grade A, Grade B, Grade C, Pass, or Fail. Once one condition matches, the remaining conditions are skipped.
```java
if (marks >= 90) {
System.out.println("Grade A");
} else if (marks >= 75) {
System.out.println("Grade B");
} else {
System.out.println("Needs Improvement");
}
```
The order of conditions matters. If you put a broad condition first, it may catch values before a more specific condition gets a chance to run.
Nested if
Nested `if` means writing one `if` statement inside another `if` statement. It is useful when a second condition should be checked only after the first condition is already true. For example, a login system may first check whether the username exists. Only after that, it checks whether the password is correct.
```java
if (usernameCorrect) {
if (passwordCorrect) {
System.out.println("Login successful");
}
}
```
Nested conditions should be used carefully. If too many `if` blocks are placed inside each other, the code becomes harder to read.
switch Statement
`switch` is useful when one variable is compared with many fixed values. It is cleaner than writing many `else if` conditions when the choices are simple and fixed. It is often used in menu-driven programs, day number programs, calculator choices, and command-based logic.
```java
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
default:
System.out.println("Invalid day");
}
```
`break` stops the switch after a matching case runs. Without `break`, Java may continue running the next cases, which is usually not what beginners want. `default` runs when none of the cases match. It is similar to the final `else` in an `if else` ladder.
Choosing Between if else and switch
Use `if else` when conditions involve ranges or comparisons, such as `marks >= 75`, `age < 18`, or `number % 2 == 0`. Use `switch` when one value is matched against fixed choices, such as menu option `1`, `2`, `3`, or day number `1` to `7`.
Example Program
```java
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in); System.out.print("Enter marks: ");
int marks = sc.nextInt(); if (marks >= 90) {
System.out.println("Grade A");
} else if (marks >= 75) {
System.out.println("Grade B");
} else if (marks >= 50) {
System.out.println("Grade C");
} else if (marks >= 35) {
System.out.println("Pass");
} else {
System.out.println("Fail");
}
}
}
```
This program uses an `else if` ladder because marks are checked in ranges. Java checks the highest grade first, then moves downward. This order keeps the grading logic correct.
Conditional Statements in DSA
Conditional statements appear in almost every DSA problem. You use them to compare values, update the maximum or minimum, check whether an element is found, handle edge cases, and control loop behavior. For example, while finding the largest element in an array, an `if` condition checks whether the current value is greater than the current maximum. Without conditions, the program cannot make decisions.
Practice Question
Take a number as input and check whether it is positive, negative, or zero. Then extend the program to check whether a positive number is even or odd.