Skip to main content
Java Basics
CHAPTER 07 Beginner

Conditional Statements

Updated: May 17, 2026
5 min read

# CHAPTER 7

Conditional Statements

1. Introduction

Programs need to make decisions. Should the ATM allow a withdrawal? Is the password correct? Is the student passing or failing? Conditional statements allow your program to choose different paths of execution based on conditions. This is the foundation of all intelligent software.

2. Learning Objectives

  • Use if, if-else, and else if statements.
  • Implement nested conditions.
  • Use the switch statement for multi-way branching.
  • Build a grade evaluation system.

3. The if Statement

Executes a block of code only if the condition is true.
java
1234
int age = 20;
if (age >= 18) {
    System.out.println("You are an adult.");
}

4. The if-else Statement

Provides an alternative path when the condition is false.
java
123456
int temperature = 35;
if (temperature > 30) {
    System.out.println("It's hot outside!");
} else {
    System.out.println("The weather is pleasant.");
}

5. The else if Ladder

Tests multiple conditions sequentially.
java
123456789101112
int score = 75;
if (score >= 90) {
    System.out.println("Grade: A");
} else if (score >= 80) {
    System.out.println("Grade: B");
} else if (score >= 70) {
    System.out.println("Grade: C");
} else if (score >= 60) {
    System.out.println("Grade: D");
} else {
    System.out.println("Grade: F");
}

6. Nested if Statements

An if inside another if.
java
123456789101112
int age = 25;
boolean hasLicense = true;

if (age >= 18) {
    if (hasLicense) {
        System.out.println("You can drive.");
    } else {
        System.out.println("Get a license first.");
    }
} else {
    System.out.println("You are too young to drive.");
}

7. The switch Statement

Ideal for comparing one variable against many exact values.
java
1234567891011121314151617181920
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;
    default:
        System.out.println("Weekend!");
}

Important: Without break, execution "falls through" to the next case!

8. Enhanced Switch (Java 14+)

java
1234567
String day = "MONDAY";
String type = switch (day) {
    case "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY" -> "Weekday";
    case "SATURDAY", "SUNDAY" -> "Weekend";
    default -> "Invalid";
};
System.out.println(type); // "Weekday"

9. Mini Project: Grade Evaluation System

java
123456789101112131415161718192021222324252627282930313233343536373839404142434445
import java.util.Scanner;

public class GradeEvaluator {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.println("=== GRADE EVALUATION SYSTEM ===");
        System.out.print("Enter your score (0-100): ");
        int score = sc.nextInt();

        String grade;
        String remark;

        if (score < 0 || score > 100) {
            grade = "Invalid";
            remark = "Please enter a score between 0 and 100.";
        } else if (score >= 90) {
            grade = "A+";
            remark = "Outstanding! You are a top performer.";
        } else if (score >= 80) {
            grade = "A";
            remark = "Excellent work!";
        } else if (score >= 70) {
            grade = "B";
            remark = "Good job, keep improving!";
        } else if (score >= 60) {
            grade = "C";
            remark = "You passed. Study harder!";
        } else if (score >= 50) {
            grade = "D";
            remark = "Just passed. Needs improvement.";
        } else {
            grade = "F";
            remark = "Failed. Please retake the course.";
        }

        System.out.println("\n--- RESULT ---");
        System.out.println("Score:  " + score);
        System.out.println("Grade:  " + grade);
        System.out.println("Remark: " + remark);
        System.out.println("--------------");

        sc.close();
    }
}

10. Common Mistakes

  • Using = instead of ==: if (x = 5) assigns; if (x == 5) compares.
  • Missing break in switch: Causes fall-through behavior.
  • Comparing Strings with ==: Use .equals() for String comparison.

11. Exercises

  1. 1. Write a program that checks if a number is positive, negative, or zero.
  1. 2. Build a simple calculator using switch-case (add, subtract, multiply, divide).
  1. 3. Write a program that takes a month number (1-12) and prints the season.

12. MCQ Quiz with Answers

Question 1

What keyword provides the default case in a switch?

Question 2

What happens without break in a switch case?

Question 3

Which operator is used for comparison?

Question 4

Can switch work with Strings?

Question 5

What does else if allow?

Question 6

What is the output of if(false) { print("A"); } else { print("B"); }?

Question 7

Can you nest if statements?

Question 8

How do you compare strings in Java?

Question 9

What does (x > 5 && x < 10) mean?

Question 10

Switch can work with which types?

13. Interview Questions

  • Q: What is the difference between if-else and switch?
  • Q: What is fall-through in switch? Is it ever useful?
  • Q: Why should you use .equals() instead of == for Strings?

14. Summary

Conditional statements (if, else if, else, switch) enable decision-making in Java programs. The switch statement is cleaner for comparing one variable against many values. Always use .equals() for String comparisons and always include break in switch cases unless fall-through is intentional.

15. Next Chapter Recommendation

In Chapter 8: Loops in Java, we'll learn how to repeat actions using for, while, and do-while loops.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·