Перейти к основному содержимому
eLearner.app
Модуль 1 · Урок 2 из 32/18 в курсе~8 min
Уроки модуля (2/3)

Операторы

Operators in C++ are used to perform operations on variables and values. They are mainly divided into four categories: arithmetic, assignment, relational, and logical.

Arithmetic Operators

These are used to perform common mathematical operations:

OperatorOperationExample
+Addition5 + 3 (8)
-Subtraction5 - 3 (2)
*Multiplication5 * 3 (15)
/Division10 / 3 (3 if integers, 3.333 if float/double)
%Modulo (remainder of integer division)10 % 3 (1)

Assignment Operators

Used to assign values to variables. C++ also supports compound assignment operators:

Code
int x = 10;
x += 5; // Equivalent to x = x + 5 (15)
x *= 2; // Equivalent to x = x * 2 (30)

Relational Operators (Comparison)

These return a boolean value (true or false):

  • == Equal to
  • != Not equal to
  • > Greater than
  • < Less than
  • >= Greater than or equal to
  • <= Less than or equal to
Code
bool result = (10 > 5); // true

Logical Operators

Used to combine multiple boolean expressions:

  • && Logical AND: true if both expressions are true.
  • || Logical OR: true if at least one expression is true.
  • ! Logical NOT: reverses the truth value.
Code
bool isAdult = true;
bool hasTicket = false;
bool canEnter = isAdult && hasTicket; // false

Try it yourself

Упражнение#cpp.m1.l2.e1
Попыток: 0Загрузка…

Calculate the remainder of dividing 17 by 5 using the modulo % operator. Save the result in an integer variable named remainder and print it with std::cout.

Загрузка редактора…
Показать подсказку

L'operatore modulo in C++ è `%`.

Решение доступно после 3 попыток

Упражнение#cpp.m1.l2.e2
Попыток: 0Загрузка…

Given age = 20 and hasLicense = true, verify if the person can drive (canDrive). Both conditions must be true. Use the && operator, store the result in a boolean variable named canDrive, and print it.

Загрузка редактора…
Показать подсказку

Use the logical AND operator `&&` to combine the two conditions.

Решение доступно после 3 попыток