GNGiseldo Neo and Alana NeoBooks
Introduction to Programming in Java4 / 25

Conditionals

Turning rules into predictable decisions

3.1 Learning objectives

By the end of this chapter, you should be able to:

  • write decisions using if, else if, and else;
  • combine conditions with relational and logical operators;
  • choose between if and switch;
  • compare text commands regardless of letter case or surrounding spaces.

3.2 Boolean expressions

A condition chooses only one of the possible paths.

Figure 3.1. A condition chooses only one of the possible paths.

A condition evaluates to true or false. The relational operators are ==, !=, >, <, >=, and <=. The most common logical operators are && (AND), || (OR), and ! (NOT).

double grade = 7.5;
int absences = 2;

boolean passed = grade >= 7.0 && absences <= 5;

Use parentheses whenever they make intent clearer, even if language precedence already gives the right result.

3.3 Decisions with if

if (grade >= 7.0) {
    System.out.println("Passed");
} else if (grade >= 5.0) {
    System.out.println("Retake");
} else {
    System.out.println("Failed");
}

Order matters: more specific conditions must appear before broader ones.

3.4 Closed options with switch

Use switch when a single expression is compared against a small set of fixed values. Use if for ranges, compound conditions, and distinct rules.

import java.util.Locale;

public class Chapter03Conditionals {
    public static String respond(String input) {
        String command = input.strip().toLowerCase(Locale.ROOT);

        return switch (command) {
            case "hi" -> "Hello! How can I help?";
            case "help" -> "Commands: hi, tip, and exit.";
            case "tip" -> "Use names that reveal intent.";
            case "exit" -> "See you soon!";
            default -> "I did not understand. Type help.";
        };
    }

    public static void main(String[] args) {
        System.out.println(respond("  HELP  "));
    }
}

Locale.ROOT makes normalization predictable regardless of the computer’s configured locale.

3.5 Common mistakes

Comic: the indecisive chatbot.

Figure 3.2. Comic: the indecisive chatbot.

  • comparing String values with ==;
  • repeating the same condition in multiple places;
  • writing a condition that can never be true;
  • creating too many nested if levels;
  • forgetting to handle empty or unknown input.

When a decision grows too large, extract it into a method with a clear name.

3.6 Project evolution: version 3

The chatbot now recognizes hi, help, tip, and exit. The default reply guides the user instead of merely reporting an error.

The complete example is in examples/src/Chapter03Conditionals.java.

3.7 Verifiable practice

Add the about command and write a rule that classifies weekly study hours:

  • fewer than 3 hours: “starting pace”;
  • from 3 to 6 hours: “steady pace”;
  • more than 6 hours: “intensive pace”.

Test cases:

InputExpected result
HELPlist of commands
tipJava tip
unknownguidance to type help
3 hourssteady pace

Mastery check

Explain why switch was used for commands and if for hour ranges.