Зміст курсу
C++ Умовні оператори
C++ Умовні оператори
Оператор Else
Оператор else
є важливим елементом керування потоком програми. Він доповнює інструкцію if
, дозволяючи програмі виконувати різні дії залежно від того, чи задана умова є true або false. По суті, це дозволяє програмі вибирати між двома альтернативними шляхами.
else
if (condition) { // Code to be executed if the condition is true } else { // Code to be executed if the condition is false }
Let's examine the provided code. When executed, it appears to be functioning as expected. Since the variable x
is less than 10, the output correctly states: X is not greater than 10
.
main
#include <iostream> int main() { int x = 5; if (x > 10) { std::cout << "X is greater than 10" << std::endl; } std::cout << "X is not greater than 10" << std::endl; }
But try to modify the value of a variable x
to a number greater than 10, and then execute the provided code snippet once more. As you can see there are two distinct messages: one indicating that X is greater than 10
, and the other specifying that X is not greater than 10
.
The key thing to understand is that the second std::cout
statement is not part of the if
block, it is always be executed unconditionally after the if
block resulting in both messages being printed. Of course this has to be fixed and this issue can be resolved by using an else
keyword.
main
#include <iostream> int main() { int x = 5; if (x > 10) { std::cout << "X is greater than 10" << std::endl; } else { std::cout << "X is not greater than 10" << std::endl; } }
Now everything works as expected and only one of two instructions will be executed depending on the value of the x
.
Тепер все працює як очікувалося і лише одна з двох інструкцій буде виконана в залежності від значення x
.
Swipe to show code editor
- Examine the
x
andy
variables usingif
andelse
keywords. - Display the results in the console:
x is greater than y
, if x > y;x is less than y
, if x > y;x is equal to y
, if x == y.
Дякуємо за ваш відгук!
Оператор Else
Оператор else
є важливим елементом керування потоком програми. Він доповнює інструкцію if
, дозволяючи програмі виконувати різні дії залежно від того, чи задана умова є true або false. По суті, це дозволяє програмі вибирати між двома альтернативними шляхами.
else
if (condition) { // Code to be executed if the condition is true } else { // Code to be executed if the condition is false }
Let's examine the provided code. When executed, it appears to be functioning as expected. Since the variable x
is less than 10, the output correctly states: X is not greater than 10
.
main
#include <iostream> int main() { int x = 5; if (x > 10) { std::cout << "X is greater than 10" << std::endl; } std::cout << "X is not greater than 10" << std::endl; }
But try to modify the value of a variable x
to a number greater than 10, and then execute the provided code snippet once more. As you can see there are two distinct messages: one indicating that X is greater than 10
, and the other specifying that X is not greater than 10
.
The key thing to understand is that the second std::cout
statement is not part of the if
block, it is always be executed unconditionally after the if
block resulting in both messages being printed. Of course this has to be fixed and this issue can be resolved by using an else
keyword.
main
#include <iostream> int main() { int x = 5; if (x > 10) { std::cout << "X is greater than 10" << std::endl; } else { std::cout << "X is not greater than 10" << std::endl; } }
Now everything works as expected and only one of two instructions will be executed depending on the value of the x
.
Тепер все працює як очікувалося і лише одна з двох інструкцій буде виконана в залежності від значення x
.
Swipe to show code editor
- Examine the
x
andy
variables usingif
andelse
keywords. - Display the results in the console:
x is greater than y
, if x > y;x is less than y
, if x > y;x is equal to y
, if x == y.
Дякуємо за ваш відгук!