Конструкція `else if`
Окрім блоку else, умовні оператори підтримують блок else if, який можна використовувати для визначення альтернативних умов, якщо початкова умова if є false.
Загальний синтаксис виглядає так:
if(expression) {
// Code … (executed if the expression is true)
} else if(expression) {
// Fallback Code …
//(executed if the previous condition is false, and this one is true)
}
Як видно із загального синтаксису, блок else if приймає булевий вираз, який перевіряється, коли попередня умова виявляється false.
Можна об'єднувати декілька блоків else if у ланцюжок if-else if:
if(expression) {
// … (executed if the first condition is true)
} else if(expression) {
// … (executed if the first condition is false and this is true)
} else if(expression) {
// … (executed if previous conditions are false and this is true)
} else {
// … (executed if all previous conditions are false)
}
Як показано у коді вище, наприкінці можна додати блок else. Цей блок виконується лише тоді, коли всі попередні умови є false.
Наступний приклад демонструє використання цього синтаксису:
123456789let number = 50; if (number < 20) { console.log("The number is less than 20."); } else if (number === 20) { console.log("The number is exactly 20."); } else { console.log("The number is greater than 20."); }
Дякуємо за ваш відгук!
Запитати АІ
Запитати АІ
Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат
Can you explain how the else if chain works in this example?
What happens if I add more else if conditions?
Can you give more examples of using else if with different values?
Чудово!
Completion показник покращився до 1.33
Конструкція `else if`
Свайпніть щоб показати меню
Окрім блоку else, умовні оператори підтримують блок else if, який можна використовувати для визначення альтернативних умов, якщо початкова умова if є false.
Загальний синтаксис виглядає так:
if(expression) {
// Code … (executed if the expression is true)
} else if(expression) {
// Fallback Code …
//(executed if the previous condition is false, and this one is true)
}
Як видно із загального синтаксису, блок else if приймає булевий вираз, який перевіряється, коли попередня умова виявляється false.
Можна об'єднувати декілька блоків else if у ланцюжок if-else if:
if(expression) {
// … (executed if the first condition is true)
} else if(expression) {
// … (executed if the first condition is false and this is true)
} else if(expression) {
// … (executed if previous conditions are false and this is true)
} else {
// … (executed if all previous conditions are false)
}
Як показано у коді вище, наприкінці можна додати блок else. Цей блок виконується лише тоді, коли всі попередні умови є false.
Наступний приклад демонструє використання цього синтаксису:
123456789let number = 50; if (number < 20) { console.log("The number is less than 20."); } else if (number === 20) { console.log("The number is exactly 20."); } else { console.log("The number is greater than 20."); }
Дякуємо за ваш відгук!