The basics of programming in c++ for beginners

Increment and decrement in 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.

81 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:
        [code]
        а = ++variable + ++variable + ++variable + ++variable;
        [/code]
        [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. That in Dev ++ it turned out correctly, need to write like this

    int variable = 1;
    int a = 0;

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

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

    Although I still do not really understand, why, after all
    ++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 Compiler 2013 gives the result 10 (moreover, without brackets the same result). The unary prefix increment operation is performed first., besides twice. variable consecutively takes values 2, then – 3. Then there is a multiplication of 3 * 2 = 6 and then addition 3 + 1 + 6 = 10. Such constructions are better not to build, but the priorities of operations must be remembered.

  6. C++ Builder 10.2 Tokyo.
    Classic Borland Compiler: 10 (2+1+2*2) From right to left. Addition first.
    Force C++ Compiler: 9 (2*3+1+2) With levs straight. From the beginning, multiplication.
    C++==C11
    About time… About manners…

Leave a Reply

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