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. in the last example everything is simple:
    a = variable + 1 + ++variable * 2;
    break it down into simple tasks:
    int variable = 1; variable is equal to one, so:
    a = (1++ equal to 2) +1 + (1++ equal to 2) *2;
    a = 2 + 1 + 2 * 2;
    a = 5 *2; hence = 10!!!

    1. You got the correct answer by accident ))) Tell, how much will 2 + 2 * 2 ? ))

      Z.Ы. ALL prefix operations are performed first ;)

      1. Yes, exactly! error)
        multiplication has higher priority! + increment and decrement change the value of a variable, so variable will be 3…
        comes out: a = 3 + 1 +3 * 2; otherwise: 3 + 1 + (3*2) = 4 + 6 = 10!

  2. “The postfix form will be applied after other commands are executed.”

    Look here: http://ru.cppreference.com/w/cpp/language/operator_precedence

    Postfix decrement has higher priority than prefix decrement. And this is correct and meets the standard. In reality everything is much simpler than what you write here , and pro a++ + ++a already years 10 schoolchildren arguing, who didn't look beyond the nose. Different ancient compilers handle this example differently.
    But you can be sure, what
    std::cout << (a++) + ++a;
    in g++ it will return the same as
    std::cout << a++ + ++a;
    because the postfix operation always executes before.

  3. and further, children, space indents uniformly.

    This is where you started putting them: https://purecodecpp.com/archives/588 , but in the first listing write

    main() {
    setlocale...
    }

    In the second

    main() {
    setlocale...
    }

    And then you scald like this:

    if (NumberOfFinger == 1)
    cout << "\nРезультат: Большой палец \n"; // почему тут пробелов больше
    else if (NumberOfFinger == 2)
    cout << "\nРезультат: Указательный палец\n"; // чем здесь?

    I do not know about you, and this chaos immediately hurts my eyes.

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

      1) ++variable – will make variable = 2
      2) ++variable second – will make variable = 3
      3) variable * 2 means 2*3=6
      4) 3 + 1 + 6 hand 10

      1. And still somehow it’s not so because it’s variable from the very beginning 1 after the prefix increment the first one is equal to 2 that is, it turns out 2+1+3*2 perhaps due to the fact that the multiplication is performed first, the code returns to the beginning and variable first is increased again and then both parts are added then 3+1+3*2 верно

    1. yes – multiplication first. And when the first addition is performed, variable is already equal 3. Once again does not increase

Leave a Reply

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