Lesson 7 — Decision Making

Conditional Statements

Programs would be useless if they did the same thing regardless of input. Conditional statements let your code choose between different paths based on data — they are the foundation of all logic.

The if Statement

The simplest form of decision-making: if this condition is true, do this. The condition inside the parentheses must evaluate to a boolean.

JAVA — if statement
int mark = 72;

// Single if — only runs when condition is true
if (mark >= 50) {
    System.out.println("You passed this assessment.");
    System.out.println("Well done on your effort.");
}

// Without braces (only safe for a single statement — avoid in practice)
if (mark >= 50) System.out.println("Passed");

// Common mistake: semicolon after if creates an empty statement
// if (mark >= 50);  // BUG — the block below always runs regardless of mark
// {
//     System.out.println("This always prints!");
// }

if / else

Handle two mutually exclusive outcomes. Exactly one branch will execute — never both, never neither.

JAVA — if / else
int mark = 43;

if (mark >= 50) {
    System.out.println("Competent — you may proceed to the next module.");
} else {
    System.out.println("Not yet competent — please review and resubmit.");
    System.out.println("Minimum required: 50%  |  Your mark: " + mark + "%");
}

if / else if / else — The Grading Ladder

Chain multiple conditions to handle more than two outcomes. Java checks them from top to bottom and executes the first branch whose condition is true. If none match, the else block runs.

JAVA — Grading ladder
int mark = 74;
String grade;

if (mark >= 80) {
    grade = "Distinction";
} else if (mark >= 70) {
    grade = "Merit — Upper";
} else if (mark >= 60) {
    grade = "Merit";
} else if (mark >= 50) {
    grade = "Pass";
} else {
    grade = "Not yet competent";
}

System.out.println("Mark: " + mark + "% — " + grade);

// The ordering matters! If you wrote >= 50 first, everyone with 50+ gets "Pass"
// regardless of whether they scored 90. Always put the most restrictive condition first.

Nested if Statements

You can place an if inside another if for multi-dimensional conditions. Keep nesting shallow — deeply nested code is hard to read and test.

JAVA — Nested if
boolean hasSubmitted = true;
int mark = 72;

if (hasSubmitted) {
    if (mark >= 50) {
        System.out.println("Submitted and passed.");
    } else {
        System.out.println("Submitted but did not pass. Resubmission required.");
    }
} else {
    System.out.println("No submission received. Please contact your facilitator.");
}

The switch Statement

When you have many fixed options to choose between, a switch is cleaner than a long chain of else if. The break statement exits the switch — without it, execution falls through into the next case.

JAVA — switch
String day = "Wednesday";

switch (day) {
    case "Monday":
        System.out.println("Java fundamentals class — 09:00 to 13:00");
        break;
    case "Tuesday":
        System.out.println("Data types and operators workshop");
        break;
    case "Wednesday":
        System.out.println("Career development and CV writing");
        break;
    case "Thursday":
        System.out.println("Project work and peer code review");
        break;
    case "Friday":
        System.out.println("Presentations and facilitator feedback");
        break;
    default:
        System.out.println("No scheduled programme today — self-study recommended.");
}

Switch Fall-Through (When It's Intentional)

JAVA — Intentional fall-through
// Intentional fall-through: multiple cases, same action
int month = 4; // April
int daysInMonth;

switch (month) {
    case 1: case 3: case 5: case 7:
    case 8: case 10: case 12:
        daysInMonth = 31;
        break;
    case 4: case 6: case 9: case 11:
        daysInMonth = 30;
        break;
    case 2:
        daysInMonth = 28; // simplified — ignoring leap years
        break;
    default:
        daysInMonth = -1;
}
System.out.println("Days in month " + month + ": " + daysInMonth);

The Ternary Operator

A compact single-line if/else for simple assignments. Read it as: condition ? value if true : value if false. Use it only when both outcomes are simple — complex ternaries hurt readability.

JAVA — Ternary operator
int mark = 74;

// Ternary:
String result = (mark >= 50) ? "Pass" : "Fail";
System.out.println(result);

// Equivalent if/else:
// if (mark >= 50) { result = "Pass"; } else { result = "Fail"; }

// Nested ternary (avoid this — it is hard to read):
String grade = (mark >= 80) ? "Distinction" : (mark >= 60) ? "Merit" : "Pass/Fail";

Practice Task

Your Turn

Write a complete program that: (1) stores a learner's mark and whether they submitted on time (boolean), (2) prints the grade label (Distinction/Merit/Pass/Not yet competent), (3) prints an additional message if they submitted late but still passed (nested if), (4) uses a switch statement to print the day's schedule for a hardcoded day string. Test by changing the mark value and checking the output changes correctly.

Common Mistakes

  • Missing break in a switch case — fall-through is silent and causes all cases below to execute until the next break.
  • Semicolon after if (...) — this creates an empty if body; the block below always runs.
  • Using = instead of == in a condition — if (x = 5) is an assignment, not a comparison (compile error for int, logic bug for boolean).
  • Ordering else if from least to most restrictive — a mark of 90 would match the first >=50 and print 'Pass'. Always put the strictest condition first.
  • Comparing Strings with == in a switch — switch on String works correctly in Java 7+ and uses .equals() internally, but be cautious with variables.

Professional Tip

Every if branch should be testable in isolation. Before moving on, manually trace through your code with at least three different input values — one from each branch — and verify the output is correct.

Mini Quiz

What happens if you omit break in a switch case?