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.
I don't know if I'm right but I thought so: multiplication comes first ++a * 2 to a is added 1 and it turns out 2*2=4, then ++a + 1 = 6 +4=10
why ++a = 6? if before ++a was equal 2?
“then ++a + 1 = 6 + 4”
How often the prefix form is operated on due to its complexity ? Is it comfortable for the programmer?
Probably the second prefix has priority over the first, he, as the main member of society, then stuck 1 = 5 , but the first prefix is older +1 by rank and she stuck to him the second time she got fat by one size. Well, it turned out to be a. or oh
but no 4+1=5
5+5=10
I keep wondering why scientists say that God created the world. I think that . for them, nature is a decrement of its own value. They do not want to believe that it was created perfixally by evolution long before the appearance of variables..
Increments will be executed first as having the highest priority for the prefix form for (twice) That. variable will take a value 3, then the rest of the statements in the expression will be executed: 3 + 1 + 3*2 = 10.
a = variable + 1 + ++variable * 2;
1) ++variable + 1 = 3
2) ++variable * 2; variable first * 2= 3*2 =6
6) increment ++variable , i.e.. already 6th and tied 7.
4) 3+7 =0
excuse me , the key did not strike.
It will be 3+7=10
#include
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;
}
result: a=9
but I did it : 2 * 2 = 4;
————————— : (4 + 1) + 1;
—————————-: result 4 + 1 + 1 +4;
First action – multiplication , second – value first(ok from left to right) becomes equal 5 . And + 1. Всего 10.
It may not be true , but somehow it corresponds to the order of arithmetic operations , and then the value of the variable changes upward… Which is the right option? ??? Prompt !