Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте If Expression | Conditional Statements
Introduction to C++ | Mobile-Friendly

book
If Expression

To perform different actions depending on the different decisions we can use already mentioned logical operators and the conditional statement if.

if executes the block of code if the condition is true. In C++, it's implemented as follows:

python
if (condition) {
// code here will be executed if the condition is true
}

For example, we know how to check if the variable x is greater than y. Let’s write the message (with the statemant if) if this condition is true (x is bigger than y):

python
#include <iostream>
using namespace std;

int main() {
// Define variables
int x = 7;
int y = 5;

// Check the condition
if (x > y) {
cout << "x is bigger than y";
}
return 0;
}

As x is 7, and y is 5, x is greater than y, so our condition is true and the code will print the message "x is bigger than y".

The statement if is case sensitive. It means that usage of uppercase letters (If or IF) will cause an error.

question-icon

Accept the variables x and y as the input of the user. Compare the variables, and print the message "x is equal to y" if they are equal:

cin >> x;
cin >> y;
if
_ _ _
{
    
_ _ _
_ _ _
"x is equal to y";
}

Натисніть або перетягніть елементи та заповніть пропуски

dots
(x == y)
dots
x==y
dots
cin
dots
>>
dots
<<
dots
(x=y)
dots
cout
dots
print

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

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

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

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

Запитати АІ

expand
ChatGPT

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

some-alt