The basics of programming in c++ for beginners

Increment and decrement in C ++




the increment in C ++, decrement in c ++, increment and decrement in with ++, c ++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:

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:

increment and decrement

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,:

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:

increment and decrement

Here's a look at this example more:

Result:
increment and decrement

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 :)

Newsletter of programming:

Increment and decrement in C ++
4.4 (87.69%) 13 votes

51 thoughts on “Increment and decrement in C ++

  1. As I understand: first compiler calculates ++ variable * 2 then there are (1+1)*2=4, then he assigns variable = 4 and considers ++ variable which initially, i.e. 4 + 1 = 5, The result is: a=++variable + 1 + ++variable*2
    a= 5 + 1 + 4
    a= 10
    Correctly???

    1. The first operation performed by the increment, then multiply, and, finally, addition.
      Look operator precedence in C ++.

  2. setlocale(LC_ALL, “rus”);

    int variable = 1;
    int a = 0;

    a = variable + 1 + ++variable*2;

    a=10
    сначала выполняется: ++variable
    int variable =2
    дальше считает 2 ++variable
    после он уже int variable =3
    а теперь математика :3+1+3*2=10

  3. a = variable + 1 + ++variable * 2;

    1 a = variable + 1 + (++variable * 2);
    ++variable * 2 = ++variable будет 2, потом 2*2=4

    2 a = (++variable + 1) + ++variable * 2;
    ( ++variable) + 1 = здесь ++variable уже равен 5 поскольку в первом
    5 + 1 решении компилятор присвоил ему значение 4 then
    дополнительный инкремент ( ++ ) увеличивает его еще
    on 1, later + цифра 1, следовательно 5+1=6
    3 a = (++variable + 1) + (++variable * 2); и теперь складываем решение 1 and 2 с лева
    6 + 4 на право это второе решение 6 плюс первое решение 4 равно 6+4=10

Leave a Reply to Olej Cancel reply

Your email address will not be published. Required fields are marked *