At previous article we are considered binary operators, which is used for arithmetic operations on two operands (variables). In this lesson we will learn how to apply the unary operators, that require one operand. they are called increment ( ++ ) and decrement (––). The role of these operators in, in order to change (increase or decrease, respectively,) the value of a variable by one, and the value will be overwritten. Consider the example, which will change the value of a variable variable unit in three different ways:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #include <iostream> using namespace std; int main() { setlocale(LC_ALL, "rus"); int variable = 0; cout << variable << endl; variable = variable + 1; // обычный способ cout << variable << " - variable = variable + 1" << endl; variable += 1; // комбинированный оператор += cout << variable << " - variable += 1" << endl; variable++; // используем инкремент ++ cout << variable << " - variable++" << endl; variable--; // используем декремент -- variable--; variable--; cout << variable << " - три раза variable--" << endl; return 0; } |
After each operation in lines 11, 13 and 15. to the variable variable increased by one. As you can see, the shortest recording – a record with the increment. Below, in strings 17 – 19, We have applied three times decrement and eventually obtain the value variable reduced to 3.
Compile:
Both unary operator can have two forms: postfix ( where the operator is located just behind the variable) and prefix (the operator is located in front of the variable). In this example, we used postfix increment and decrement. What is the difference between these forms from each other? Both the first and second forms of these operators perform the same role – increase or decrease of a variable by one. But if a line of code, where the increment or decrement is applied, present any further commands, whereas these operators in different ways behave. Postfix will be used after the other teams. Let's look at the following example,:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | #include <iostream> using namespace std; int main() { setlocale(LC_ALL, "rus"); int variable = 0; // постфиксный инкремент // значение variable увеличится на 1 после показа на экран cout << variable++ << " - результат \"cout << variable++\" " << endl; cout << variable << " - изменённое значение" << endl; // префиксный инкремент // сразу увеличение на 1, потом показ на экран cout << ++variable << " - результат \"cout << ++variable\" " << endl; // та же логика для декремента // постфиксный декремент cout << variable-- << " - результат \"cout << variable--\" " << endl; cout << variable << " - изменённое значение" << endl; // префиксный декремент cout << --variable << " - результат \"cout << --variable\" " << endl; return 0; } |
As a result of lines of code 12, we will see on the screen the number of 0. It happens because, that the increase in the unit will run after the withdrawal value of the variable variable the screen. To make sure, that the value really changed – in string 13 please show us variable. But in the case of the prefix form of increment (string 16), Variable overwritten directly and already only then will display. To decrement all the same, but with a reduction unit.
Compile:
Here's a look at this example more:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #include <iostream> using namespace std; int main() { setlocale(LC_ALL, "rus"); int variable = 1; int a = 0; a = ++variable + 1 + ++variable * 2; cout << "a = " << a << endl; return 0; } |
Try to understand, why as a result of a string of operations 11, variable a It became equal 10. Asking questions in the comments not prohibited.



Help me understand the problem:
x = y = 0;
while ( Y < 10 ) x += ++ Y;
printf (" x= %d y = %d\n", x, Y);
Answer x = 55 y = 10
I don't understand how x comes out 55. I just broke my head(((
x=0+1=1
x=1+2=3
x=3+3=6
x=6+4=10
x=10+5=15
x=15+6=21
x=21+7=28
x=28+8=36
x=36+9=45
x=45+10=55
pervaya cifra eto zna4enie x a vtoraya cifra eto zna4enie y, vote in results, ++y srazu uveli4ivaet zna4enie y na 1
x=0+1=1
x=1+2=3
x=3+3=6
x=6+4=10
x=10+5=15
x=15+6=21
x=21+7=28
x=28+8=36
x=36+9=45
x=45+10=55
pervaya cifra eto zna4enie x a vtoraya cifra eto zna4enie y, vote in results, ++y srazu uveli4ivaet zna4enie y na 1
By the way, why does it turn out to be a number? 10 , in string 11 ?? I'm using CodeBlock as my environment. First I wrote the code itself, received 9. Then I specially copied yours and … again 9. I understand so, that in the process of calculation,when the second increment is processed, it is further multiplied by 2, and only after that it adds up to the left side of the team. Can you explain?, why is that ? Or that's how it should be ? Then why do you 10? And in general, The algorithm seems a little illogical…
This sequence of calculations?
a = variable + 1 + ++variable * 2
a = (2 + 1 + 2) * 2
a=++variable + 1 + ++variable*2;
a=(2+1+3)*2; \\ I understand everything correctly?
Then it turns out 12))
I don't understand! If you count correctly, it should work 7, as – 2+1+2*2 = 7.
But 10 turn out to be, if the compiler accepted the values (2+1+2) as in parentheses, why he took it that way is not clear and how to write, so as not to perceive it that way!?
But most of all I don’t understand – Why does the online compiler work at all? 9!? – I don’t even understand how he thought that! Can someone explain?
So, I understand now, that the second increment also increases – but then in the online compiler it is correct – 9, but why 10 – it's not clear!?
I.e, as far as I understand, the compiler first evaluates variable.
1. variable=1+1, assigned 2.
2. variable=2+1, assigned 3.
Now a=3+1+3×2=10 is calculated. So?
definitely didn't count “programming”, but as I thought 9 it should work – I understand that – just like in the compiler… there might be a typo or something like that….
Checked in 2 online compilers, the same parsley, a=9. But in theory it should be 10, because pre-increment has priority 3 RL, and therefore, first variable is calculated, and then he takes part in the example. And he is equal in any way 3, so the example should look like this: a=3+1+3*2. Compilers , apparently, count sequentially, i.e.. a=2+1+3*2, then it really works out 9. But why is that so?! I didn't understand something? Mother, who could explain it better?.
2+1+2*2
the second variable still holds the value 1, at the moment when the first variable is already 2. but the arithmetic is still unclear to me. multiplication must be performed first.