Conditions

Basic Java
≈ 4 min read

How a program makes decisions

Until now, the code has run from top to bottom without making a choice. Conditions let you change that path: execute one piece of code if an expression is true, and another if it is not. This is how a program starts reacting to data instead of always performing the same sequence of actions.

The if condition

The parentheses after if must contain an expression of type boolean. If it evaluates to true, the code inside the block runs. If it evaluates to false, the block is skipped.

int temperature = 28;

if (temperature > 25) {
    System.out.println("Жарко");
}

The expression temperature > 25 is already familiar from operators. The new part is if, which uses the result of that expression to decide whether to run the block.

Java does not automatically turn numbers into conditions. Writing if (temperature) will not compile: the condition must have the value true or false.

Two choices: if and else

When you need to choose one of two paths, add else to if. Exactly one of the two branches will run.

int balance = 700;
int price = 500;

if (balance >= price) {
    System.out.println("Покупка доступна");
} else {
    System.out.println("Недостаточно средств");
}

Several choices: else if

If there are more than two choices, you can check conditions one after another with else if. Java goes from top to bottom and runs the first branch whose condition is true. The remaining branches in that chain are not checked.

int score = 82;

if (score >= 90) {
    System.out.println("Отлично");
} else if (score >= 75) {
    System.out.println("Хорошо");
} else if (score >= 60) {
    System.out.println("Зачёт");
} else {
    System.out.println("Нужно повторить тему");
}

Order matters here. For the value 82, the check score >= 90 fails, the next one succeeds, and the program immediately chooses the “Хорошо” branch.

Combined and nested conditions

Inside if, you can use the logical operators you already know, and one conditional block can be placed inside another. This is useful when the second check only makes sense after the first one succeeds.

boolean loggedIn = true;
boolean isAdmin = false;

if (loggedIn) {
    if (isAdmin) {
        System.out.println("Панель администратора");
    } else {
        System.out.println("Личный кабинет");
    }
}

It is better not to make nesting too deep: if blocks start moving far to the right, the code can usually be organized more clearly. We will return to this when methods appear.

switch for choosing by one value

An else if chain is convenient for ranges and different logical expressions. But when the same result needs to be compared with several specific alternatives, switch is often clearer.

int level = 2;

int bonus = switch (level) {
    case 1 -> 100;
    case 2 -> 250;
    case 3 -> 500;
    default -> 0;
};

System.out.println(bonus);

Here, switch does not just choose a branch — it immediately returns a value that is stored in bonus. The arrow -> separates a case alternative from its result, while default handles all other values.

In older Java code, you may see a different switch style. It uses a colon after case and often uses break. For new simple constructs, the arrow form is usually easier to read and prevents accidental fall-through into the next case.

What to choose

  • if / else if / else — when the decision is based on arbitrary logical expressions, ranges, or several different checks;
  • switch — when you choose an action or value based on one result from several specific alternatives.

Both constructs solve the same problem: they let the program choose which code to execute next. In the next topic, this idea will be useful again — except instead of making a one-time choice, the code will be able to repeat actions several times.