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.


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.


increment and decrement

Here's a look at this example more:

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.6 (92%) 10 votes

38 thoughts on “Increment and decrement in C ++

  1. C ++ is no such thing, the multiplication first and then everything else.
    There is an example: 1+1*2+1 = 4 (if it is considered in mathematics, first multiplication ..)
    and if both the pros: 1+1*2+1 = 5 (Everything goes according to the order of 1 + 1 = 2 * 2 = 4 + 1 = 5)
    Clear, understandably?! Pnyatnenko? Yasnenko!?

    1. This is a joke for Christmas? ;-)

      1+1*2+1 = 4 – this not only in C ++, but also in all dozens of different programming languages. Everywhere!

    1. Yes. Initially, the increment increases the value of the variable, Further, all operations in succession, both taught in school.

      1. I will describe more clearly, maybe he long delved into the essence)
        In fact, it turns out as a: First, the program performs a change in the variable ( ie if ++ variable is found 3 fold, and the value of the variable variable becomes equal to 4), and only then inserts this calculated value 4 in our example, and then everything goes according to mathematical laws.
        For example: [code]
        int variable = 1;
        int a = 0;
        а = ++variable + ++variable + ++variable + ++variable;
        cout << "a = " << a < 5+5+5+5=20

      2. Expressions of the type shown – it is nonsense:
        а = ++variable + ++variable + ++variable + ++variable;
        [b]Standard[/b] language C ++ [b]warns[/b], that the result of such “experiments” [b]is unpredictable and depends on the compiler[/b], not tolerated even among versions of the compiler, and [b]should not[/b] used.

  2. The compiler Dev-C ++ gives the result 9, and it is logical, t. to. first defined the first term 1+1, then the third (1+1+1)*2

    1. I agree! answer 9 but not 10.
      2+1+3*2 =9 , which develops from the beginning 2 and 1, and then to this sum is added the product 3*2

  3. response will depend on the compiler, if you are in the design practice to use your hands beat off

  4. Чтобы в Dev++ получилось правильно, нужно писать так

    int variable = 1;
    int a = 0;

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

    cout << "a = " << a << endl;

    Хотя всё равно не очень понимаю, why, ведь
    ++variable = 2

    2+(1+ (2+1)*2)
    2 + (1 + (3*2))
    2 + 7 = 9

  5. int variable = 1;
    int a = 0;

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

    cout << "a = " << a << endl;
    Компилятор Microsoft Visual Studio Express 2013 дает результат 10 (притом, без скобок тот же результат). Унарная операция префиксного инкремента выполняется первой, притом дважды. variable последовательно принимает значения 2, затем – 3. Затем происходит умножение 3*2=6 и потом сложение 3+1+6=10. Такие конструкции лучше не строить, но о приоритетах операций нужно помнить.

  6. C++ Builder 10.2 Tokyo.
    Classic Borland Compiler: 10 (2+1+2*2) С права налево. Сначала сложение.
    Force C++ Compiler: 9 (2*3+1+2) С лева направо. С начала умножение.
    О временаО нравы

Leave a Reply

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