Оператор if-else
if-else
Оператор if-else
є основою в програмуванні. Він дозволяє керувати потоком вашої програми на основі певних умов.
Примітка
Зверніть увагу! Блоки коду
{ }
були введені в Розділі 1, Главі 2.
Структура if-else
досить проста:
if (condition)
{
// Execute this block of code if condition is true
}
else
{
// Execute this block of code if condition is false
}
Візьмемо, наприклад, змінну temperature
, яка отримує дані від датчика температури.
Припустимо, ми хочемо програму, яка попереджає нас, коли температура стає занадто високою:
Main.c
123456789101112131415161718#include <stdio.h> int main() { int temperature = 200; // in celsius if (temperature > 80) { printf("Temperature is so high: %d degrees Celsius\n", temperature); } else { printf("Temperature is normal: %d degrees Celsius\n", temperature); } return 0; }
Ви можете включити декілька операторів if
у програму, особливо коли потрібно оцінити різні умови.
Примітка
Умови також можуть включати логічні оператори.
Else If
Оператор if-else
може бути розширений за допомогою else-if
:
Main.c
12345678910111213141516171819202122232425262728#include <stdio.h> int main() { int tempereture = 50; // in celsius if (tempereture > 50) // condition 1 { printf("Temperature is high: %d degrees Celsius\n", tempereture); // instruction_1 } else if (tempereture > 100)// condition 2 { printf("Temperature is so high: %d degrees Celsius\n", tempereture);// instruction_2 } else if (tempereture > 150)// condition 3 { printf("Temperature is critically high: %d degrees Celsius\n", tempereture);// instruction_3 } else { printf("Temperature is normal: %d degrees Celsius\n", tempereture); // instruction_4 } return 0; }
1. Яка мета оператора if-else у програмуванні?
2. Яка базова структура оператора if-else в C, включаючи синтаксис для блоків коду.
Дякуємо за ваш відгук!
Запитати АІ
Запитати АІ
Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат
Awesome!
Completion rate improved to 2.63
Оператор if-else
Свайпніть щоб показати меню
if-else
Оператор if-else
є основою в програмуванні. Він дозволяє керувати потоком вашої програми на основі певних умов.
Примітка
Зверніть увагу! Блоки коду
{ }
були введені в Розділі 1, Главі 2.
Структура if-else
досить проста:
if (condition)
{
// Execute this block of code if condition is true
}
else
{
// Execute this block of code if condition is false
}
Візьмемо, наприклад, змінну temperature
, яка отримує дані від датчика температури.
Припустимо, ми хочемо програму, яка попереджає нас, коли температура стає занадто високою:
Main.c
123456789101112131415161718#include <stdio.h> int main() { int temperature = 200; // in celsius if (temperature > 80) { printf("Temperature is so high: %d degrees Celsius\n", temperature); } else { printf("Temperature is normal: %d degrees Celsius\n", temperature); } return 0; }
Ви можете включити декілька операторів if
у програму, особливо коли потрібно оцінити різні умови.
Примітка
Умови також можуть включати логічні оператори.
Else If
Оператор if-else
може бути розширений за допомогою else-if
:
Main.c
12345678910111213141516171819202122232425262728#include <stdio.h> int main() { int tempereture = 50; // in celsius if (tempereture > 50) // condition 1 { printf("Temperature is high: %d degrees Celsius\n", tempereture); // instruction_1 } else if (tempereture > 100)// condition 2 { printf("Temperature is so high: %d degrees Celsius\n", tempereture);// instruction_2 } else if (tempereture > 150)// condition 3 { printf("Temperature is critically high: %d degrees Celsius\n", tempereture);// instruction_3 } else { printf("Temperature is normal: %d degrees Celsius\n", tempereture); // instruction_4 } return 0; }
1. Яка мета оператора if-else у програмуванні?
2. Яка базова структура оператора if-else в C, включаючи синтаксис для блоків коду.
Дякуємо за ваш відгук!