Conditional Statements in Java
Introduction
Conditional statements are used to make decisions in a Java program. They allow a program to execute different blocks of code depending on whether a condition is true or false.
For example:
int age = 20; if (age >= 18) { System.out.println("You are an adult."); }
Here, the message is displayed only when age >= 18 is true.
Types of Conditional Statements
Java provides the following common conditional statements:
Conditional Statements │ ├── if statement ├── if-else statement ├── if-else-if ladder ├── nested if statement └── switch statement
1. if Statement
The if statement executes a block of code only when a condition is true.
Syntax
if (condition) { // statements }
Example
public class Main { public static void main(String[] args) { int age = 20; if (age >= 18) { System.out.println("You are an adult."); } } }
Output:
You are an adult.
Flow
Condition | +----+----+ true false | | Execute Skip | ↓ Continue
2. if-else Statement
The if-else statement executes one block when the condition is true and another block when it is false.
Syntax
if (condition) { // statements if true } else { // statements if false }
Example
public class Main { public static void main(String[] args) { int age = 16; if (age >= 18) { System.out.println("You can vote."); } else { System.out.println("You cannot vote."); } } }
Output:
You cannot vote.
3. if-else-if Ladder
The if-else-if ladder is used when there are multiple conditions to check.
Syntax
if (condition1) { // statements } else if (condition2) { // statements } else if (condition3) { // statements } else { // statements }
Java checks the conditions from top to bottom. Once it finds a true condition, its block is executed and the remaining conditions are skipped.
Example
public class Main { 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 A"); } else if (marks >= 70) { System.out.println("Grade B"); } else if (marks >= 60) { System.out.println("Grade C"); } else { System.out.println("Fail"); } } }
Output:
Grade A
4. Nested if Statement
An if statement inside another if statement is called a nested if statement.
Syntax
if (condition1) { if (condition2) { // statements } }
Example
public class Main { public static void main(String[] args) { int age = 20; boolean citizen = true; if (age >= 18) { if (citizen) { System.out.println("Eligible."); } } } }
Output:
Eligible.
5. switch Statement
The switch statement is useful when you want to choose between multiple fixed values of an expression.
Syntax
switch (expression) { case value1: // statements break; case value2: // statements break; default: // statements }
Example
public class Main { public static void main(String[] args) { int day = 2; switch (day) { case 1: System.out.println("Sunday"); break; case 2: System.out.println("Monday"); break; case 3: System.out.println("Tuesday"); break; default: System.out.println("Invalid day"); } } }
Output:
Monday
Why use break?
break stops execution from continuing into the next case.
Without break, Java may continue executing subsequent cases until it reaches a break or the end of the switch.
6. default in switch
The default block executes when none of the cases match.
int day = 10; switch (day) { case 1: System.out.println("Sunday"); break; case 2: System.out.println("Monday"); break; default: System.out.println("Invalid day"); }
Output:
Invalid day
7. Ternary Operator
The ternary operator is a short way to write a simple if-else expression.
Syntax
condition ? valueIfTrue : valueIfFalse;
Example
int age = 20; String result = (age >= 18) ? "Adult" : "Minor"; System.out.println(result);
Output:
Adult
The ternary operator is best used for simple choices. For complex logic, if-else is usually easier to read.
8. Using Logical Operators in Conditions
Conditional statements can use logical operators.
AND (&&)
Both conditions must be true.
int age = 25; if (age >= 18 && age <= 60) { System.out.println("Eligible age range."); }
OR (||)
At least one condition must be true.
int day = 6; if (day == 6 || day == 7) { System.out.println("Weekend."); }
NOT (!)
Reverses a boolean condition.
boolean raining = false; if (!raining) { System.out.println("You can go outside."); }
9. Comparison Operators in Conditions
Conditions commonly use relational operators.
== Equal to != Not equal to > Greater than < Less than >= Greater than or equal to <= Less than or equal to
Example:
int marks = 75; if (marks >= 40) { System.out.println("Pass"); } else { System.out.println("Fail"); }
Output:
Pass
10. Complete Example
The following program checks a student's grade.
import java.util.Scanner; public class StudentGrade { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter your marks: "); int marks = sc.nextInt(); if (marks < 0 || marks > 100) { System.out.println("Invalid marks."); } else if (marks >= 90) { System.out.println("Grade: A+"); } else if (marks >= 80) { System.out.println("Grade: A"); } else if (marks >= 70) { System.out.println("Grade: B"); } else if (marks >= 60) { System.out.println("Grade: C"); } else if (marks >= 40) { System.out.println("Grade: D"); } else { System.out.println("Result: Fail"); } sc.close(); } }
Example:
Enter your marks: 85 Grade: A
11. Difference Between Conditional Statements
| Statement | Use |
|---|---|
if | Checks one condition |
if-else | Chooses between two possibilities |
if-else-if | Checks multiple conditions |
Nested if | Places one condition inside another |
switch | Chooses among multiple fixed cases |
Ternary ?: | Short form for a simple if-else |
12. Conditional Statement Flow
Start | ↓ Check Condition / \ true false | | ↓ ↓ Execute Block Else Block \ / \ / ↓ ↓ Continue
Conclusion
Conditional statements allow Java programs to make decisions based on conditions. The most important conditional structures are if, if-else, if-else-if, nested if, and switch. The ternary operator can also be used for simple conditional expressions. These statements are essential for creating programs that respond differently to different inputs and situations.