# Operators of increment (++) and decrement (–).The complex assignment operators (+ =, – =, …)

## Operators of increment (++) and decrement (—).The complex assignment operators (+=, -=, …)

### Contents

##### 1. What are the features of using increment and decrement operators in C ++ programs?

In C ++, two operators are defined that increase or decrease the value by 1:

• the ‘++’ operator – increment;
• the ‘–‘ operator – decrement.

These operators are unary. They require one operand. These operators can be placed before and after the operand.

The increment operator ++ increments the value of the operand by 1. For example, a string

`x = x + 1;`

is the same as the string

`x++;`

or

`++x;`

Also, the decrement operator reduces the value of the operand by 1. For example, a string

`x = x – 1;`

can be written

`x--;`

or

`--x;`

##### 2. Examples of using the increment (++) and decrement (—) operators

A snippet of code that explains the operation of ‘++’ and ‘–‘.

```// the operators of increment (++) and decrement (--)
int a, b;

a = 10;
b = a++; // b = 10; a = 11

a = 10;
b = ++a; // b = 11; a = 11

a = 10;
b = a--; // b = 10; a = 9

a = 10;
b = --a; // b = 9; a = 9```

##### 3. What is the difference between the expression ++x (–x) and the expression x++ (x–)?

The difference between the prefix and postfix forms of the increment (++) and decrement operators (), is manifested when these operators participate in the assignment operator.

If the prefix expression ++x is used in the assignment operator

`y = ++x;`

then it works as follows:

• first, the value of x is incremented by 1, and then the resulting value is assigned to the variable y.

If you execute the postfix form of the increment operator

`y = x++;`

then in this case:

• first, the variable y is assigned the value of x, and then the value of x is incremented by 1.

##### 4. What compound assignment operators are used in C++?

In C++, you can use the compound assignment operators. These operators are handy when long variable names are used in the program. In this case, you do not need to enter the long variable name again.

The general view of the compound operator of assignment is as follows:

`variable_name operation= expression;`

where

• variable_name – directly the name of the variable to which the value is assigned;
• operation – one of the operations +, , *, /, %, &, |, <<, >>.

The C++ language supports the following compound assignment operators:

`+=, -=, *=, /=, %=, &=, |=, =, <<=, >>=`

##### 5. Examples of using the compound assignment operators
```// composite assignment operators
float x, y;
int a, b;

// +=, -=
a = 8;
b = 5;
a += b; // a = a + b = 13
b -= 4; // b = b - 4 = 1

// *=, /=
x = 4;
y = 5;
x *= y; // x = x * y = 4 * 5 = 20
y /= 2.5; // y = y / 2.5 = 2.0

// &=, |=
a = 8;
b = 3;
a &= b + 5; // a = a & b + 5 = a & (b+5) = 8
a |= b; // a = a | b = 11

// >>=, <<=
a = 34;
a >>= 1; // a = a >> 1 = 17
a = 6;
a <<= 3; // a = a << 3 = 48

// %=
b = 15;
b %= 6; // b = b % 6 = 3```

##### 6. Is it possible to apply the increment (++) and decrement (—) operations for type bool?

In Visual C++, only the increment operation (++) can be applied for the bool type.

If you use the decrement () operation to the bool type, then the compiler generates an error message:

`Not allowed on operand of type 'bool'`

An example of using an increment operation on a variable of ‘bool’ type

```bool b;
b = false;
b++; // b = true
b++; // b = true```

##### 7. Is it possible to use increment and decrement operations on real types (float, double, long double)?

With variables of the real type, the increment and decrement operations work exactly the same as with the variables integer type.

Example.

```float x;

x = 24.5;
x--; // x = 23.5```

##### 8. Is it possible to use the increment and decrement operators to the variables of character type (char)?

Since the character type (char) implicitly converts to the integer type, the character type can be used for increment and decrement operations.

An example.

```char c;

c = 'x';
c++; // c = 'y'

c = '6';
--c; // c = '5'```