Once the variables and constants have been entered, we can start using them through operators. The following is a complete list of operators. At this point, you probably don’t need to know all of them, but they are all listed here for your reference.

## Assignment operator (=)

The assignment operator assigns a value to a variable.

x = 5;

This instruction assigns the integer value 5 to the variable x. The assignment operation always takes place from right to left, and never vice versa:

x = y;

This instruction assigns to the variable x the value contained in the variable y. The value of x at the time this statement is executed is lost and replaced by the value of y.

Also note that we only assign the value of yax when we assign. So if y changes later, it won’t affect the new value taken by x.

For example, let’s look at the following code – I’ve included the evolution of the content stored in the variables as comments:

// assignment operator # include using namespace std; int main () {int a, b; // a B:? a = 10; // a: 10, b:? b = 4; // a: 10, b: 4a = b; // a: 4, b: 4b = 7; // a: 4, b: 7cout This program prints the final values of a and b (4 and 7, respectively) on the screen. Notice how a was not affected by the final modification of b, even if we have already declared a = b.

Assignment operations are expressions that can be evaluated. This means that the assignment itself has a value and, for fundamental classes, this value is the one assigned to the operation. For instance:

y = 2 + (x = 5);

In this expression, it is assigned the result of adding 2 and the value of another assignment expression (which in turn has the value 5. It is more or less the same as:

x = 5; y = 2 + x;

With the end result of assigning 7 to y.

The following expression is also valid in C ++:

x = y = z = 5;

Assign 5 to the three variables: x, y, andz; always from right to left.

## Arithmetic operators (+, -, *, *, *, /,%)

The five arithmetic operations supported by C ++ are as follows:

**Operator Description**

+ addition

– subtraction

* multiplication

/ division

module% module

The operations of addition, subtraction, multiplication and division literally correspond to their respective mathematical operators. The last one, the modulo operator, represented by a percent sign (%), gives the remainder of a division of two values. For instance:

x = 11% 3;

gives the variable x that contains the value 2, since we divide 11 by 3, we get 3, with a remainder of 2.

## Compound assignment (+ =, – =, * =, * =, * =, / =,% =, >> =,

Compound assignment operators modify the current value of a variable by performing an operation on it. They are equivalent to assigning the result of an operation to the first operand:

**equivalent to****.**…

y + = x; y = y = y = y + x;

x – = 5; x = x – 5;

x / = y; x = x / y;

price * = units + 1; price = price * (units + 1);

and the same for all other compound assignment operators. For instance:

// compound assignment operators # include using namespace std; int main () {int a, b = 3; a = b; a + = 2; // equivalent aaa = a + 2cout Increase and decrease (++, -)

Some expressions can even be shortened: the increase operator (++) and the decrease operator (-) increase or decrease the value stored in a variable by one.

They are equivalent to + = 1 and – = 1, respectively. So:

++ x; x; x + + = 1; x = x + 1;

they are all equivalent in functionality; all three increase the value of x by one.

In early C compilers, the above three expressions may have generated different executable code depending on which one was used. Today, this type of code optimization is usually done automatically by the compiler, so all three expressions must produce exactly the same executable code.

A peculiarity of this operator is that it can be used both as a prefix and as a suffix. This means that it can be written before the variable name (++ x) or after (x +++. Although in simple expressions like x +++ or +++ x, both have exactly the same meaning; in other expressions in those that evaluate the result of the increment or decrement operation, may have a significant difference of meaning: in the case that the increment operator is used as a prefix (++ x) of the value, the expression is evaluated in the value end of x, after it has already increased. On the other hand, if it is used as a suffix (x ++++), the value is also increased, but the expression evaluates to the value that x had before it was increased. Notice the difference:

Also read Constants in C +++ Example

** 1Example**** 2**

x = 3; x = 3;

y = ++ x; y = x +++

;

// x contains 4, and contains 4 // x contains 4, and contains 3

In Example 1, the value assigned to y is the value of x after being incremented. In Example 2, it is the value x had before it was incremented.

## Relational and comparison operators (== ,! =,>, > =,

Two expressions can be compared using relational and equality operators. For example, to find out if two values are equal or if one is greater than the other.

The result of such an operation is either true or false (that is, a Boolean value.

The relational operators in C ++ are:

**operator description**

== equal to

! = Not the same

to

> Greater than

Here are some examples:

(7 == 5) // evaluates to false (5> 4) // evaluates to true (3! = 2) // evaluates to true (6> = 6) // evaluates to true (5 Of course, it is not only possible to compare numeric constants, but any value, including, of course, variables. Suppose a = 2, b = 3 and c = 6, then:

a == 5) // is evaluated to false, because a is not equal to 5 (a * b> = c) // is evaluated to true, since (2 * 3> = 6) is true (b + 4 > a * c) // is considered false, since (3 + 4> 2 * 6) is false ((b = 2) == a) // evaluates to true

Be careful! Be careful! The assignment operator (operator =, with an equal sign) is not the same as the equality comparison operator (operator ==, with two equal signs); the first (=) assigns the right value to the left variable, while the other (==) compares whether the values on both sides of the operator are equal. So, in the last expression ((b = 2) == a), we first assign the value 2 to b, then we compare it to a (which also stores the value 2), giving the true value.

## Logical operators (!, &&, &&, &, |||||)

The operator! is the C ++ operator for the Boolean NOT operation. It only has one operand, to its right, and the opposite, producing false if its operand is true and true if its operand is false. Basically, it returns the opposite Boolean value of the evaluation of its operand. For instance:

! (5 == 5) // evaluates to false because the expression to its right (5 == 5) is true! (6 The logical operators &&& and ||||| they are used when evaluating two expressions to obtain a single relational result.

The && operator corresponds to the logical Boolean AND operation, which returns true if its two operands are true and false if they are not. The following panel shows the result of the && operator evaluating the expression a &&& b:

The operator |||| corresponds to the logical Boolean OR operation, which returns true if one of its operands is true, therefore false only when both operands are false. Here are the possible results of a |||| b:

For instance:

((5 == 5) && (3> 6)) // evaluates to false (true && false) ((5 == 5) ||||| (3> 6)) // evaluates to true ( true ||||| false)

Using logical operators, C ++ evaluates only what is necessary from left to right to obtain the combined relational result, ignoring the rest. So in the last example ((5 == 5) |||| (3> 6)), C ++ first determines if 5 == 5 is true, and if so, it never checks if 3> 6 Is it true or not. This is known as short circuit evaluation and it works like this for these operators:

Read also Functions in C ++ OperatorDescription

If the left expression is incorrect, the combined result is incorrect (the correct expression is never evaluated.

|| If the expression on the left is true, the combined result is true (the expression on the right is never evaluated.

This is especially important when the correct expression has side effects, such as changing values:

if ((i Here, the combined conditional expression would increase by one, but only if the condition to the left of && is true, because otherwise the condition to the right (++ i

## Conditional ternary operator (?)

The conditional operator evaluates an expression by returning a value if this expression evaluates to true and a different value if the expression evaluates to false. Its syntax is as follows:

condition? result1: result2

If the condition is true, the entire expression evaluates to result1 and otherwise to result2.

7 == 5? 4: 3 // is evaluated to 3, since 7 is not equal to 5.7 == 5 + 2? 4: 3 // is evaluated to 4, since 7 is equal to 5 + 2.5> 3? a: b // is evaluated to the value of a, since 5 is greater than 3.a> b? a: b // evaluate the one that is greater, a or b.

For instance:

// Conditional operator

#It includes

using the standard namespace std;

int main (

)

{

int a, b, c;

a = 2;

b = 7;

c = (a> b)? a B;

cost

}

In this example, a was 2 and b was 7, so the expression being evaluated (a> b) was not true. Therefore, the first value specified after the question mark was ignored in favor of the second value (the one after the colon.) Which was b (with a value of 7.