Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте else if Chains in Java | Оператори та умовні конструкції
Вступ до Java

else if Chains in Java

Свайпніть щоб показати меню

So far, if-else statements work great when only two outcomes are needed. But in real systems, decisions are usually more detailed than that.

For example, a coffee shop might have different reward levels depending on how many items someone orders:

  • small orders get nothing extra;
  • medium orders get a small bonus;
  • large orders get a bigger reward;
  • very large orders might unlock a special offer.

This is where else if chains become useful — they allow us to check multiple conditions one after another. The program goes from top to bottom, and as soon as it finds a condition that is true, it runs that block and ignores everything else.

Structure

if (condition1) {
    // runs if condition1 is true
} else if (condition2) {
    // runs if condition2 is true
} else {
    // runs if none are true
}

Only one block will ever run — the first matching condition wins.

Example

Main.java

Main.java

123456789101112131415161718
package com.example; public class Main { public static void main(String[] args) { int deliveryDistance = 7; if (deliveryDistance > 20) { System.out.println("Delivery fee: 10€"); } else if (deliveryDistance > 10) { System.out.println("Delivery fee: 5€"); } else if (deliveryDistance > 5) { System.out.println("Delivery fee: 2€"); } else { System.out.println("Free delivery"); } } }

You check deliveryDistance against each condition from top to bottom. Since 7 is not greater than 20 or 10, but is greater than 5, the third block matches and prints "Delivery fee: 2€". The else at the bottom would only run if none of the conditions were met.

One very important thing to understand is that order matters. Since Java stops at the first true condition, structure your checks from the most strict to the most general — otherwise a broader condition could accidentally catch values meant for a more specific rule.

Think of else if chains as a decision ladder — the program climbs down step by step until it finds the correct match.

question mark

What will this print?

Виберіть правильну відповідь

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 3. Розділ 5

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

else if Chains in Java

So far, if-else statements work great when only two outcomes are needed. But in real systems, decisions are usually more detailed than that.

For example, a coffee shop might have different reward levels depending on how many items someone orders:

  • small orders get nothing extra;
  • medium orders get a small bonus;
  • large orders get a bigger reward;
  • very large orders might unlock a special offer.

This is where else if chains become useful — they allow us to check multiple conditions one after another. The program goes from top to bottom, and as soon as it finds a condition that is true, it runs that block and ignores everything else.

Structure

if (condition1) {
    // runs if condition1 is true
} else if (condition2) {
    // runs if condition2 is true
} else {
    // runs if none are true
}

Only one block will ever run — the first matching condition wins.

Example

Main.java

Main.java

123456789101112131415161718
package com.example; public class Main { public static void main(String[] args) { int deliveryDistance = 7; if (deliveryDistance > 20) { System.out.println("Delivery fee: 10€"); } else if (deliveryDistance > 10) { System.out.println("Delivery fee: 5€"); } else if (deliveryDistance > 5) { System.out.println("Delivery fee: 2€"); } else { System.out.println("Free delivery"); } } }

You check deliveryDistance against each condition from top to bottom. Since 7 is not greater than 20 or 10, but is greater than 5, the third block matches and prints "Delivery fee: 2€". The else at the bottom would only run if none of the conditions were met.

One very important thing to understand is that order matters. Since Java stops at the first true condition, structure your checks from the most strict to the most general — otherwise a broader condition could accidentally catch values meant for a more specific rule.

Think of else if chains as a decision ladder — the program climbs down step by step until it finds the correct match.

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 3. Розділ 5
some-alt