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.

80 thoughts on “Increment and decrement in C ++

  1. and if the growth rates and a bunch of everything and the addition and subtraction and multiplication in the end it was,how to move from the bottom up or top down after multiplication

  2. ++variable + 1 + ++variable * 2=( 2+1+2) *2=10
    1-th action: ++variable;
    2-th action: ++variable;
    3-It is 4-OE action: with the addition of a unit;
    5-th action: *2.

    1. Here you are a little bit wrong, actions are otherwise:
      1) ++variable; 1+1=2
      2)++variable; 2+1= 3, wherein the first variable value also changes
      3)Total obtain 3 + 1 + 3 * 2 = 10

      1. I disagree:
        1) ++variable; 1+1=2
        2)++variable; 2+1= 3, wherein the first variable value also changes – Does not change!
        3)total we get 2 + 1 + 3 * 2 = 9

    1. In the C standard, between two points of successive calculations, a change in the value of a variable is possible no more than once..
      In this case, the variable variable changes 2 times in one expression. And in what order will the calculation go – depends on the compiler version:
      or so: ++var(var = 2), ++var(var = 3), a=3+1+3*2=10
      or so: ++var(var = 2), a=2+1+(++var)*2=2+1+3*2=9
      my compiler also gave an answer 9.
      Therefore, such constructs should never be used., if only for educational purposes

  3. And what about the count's such code?

    int variable = 1;
    int a = 0;
    a = –variable + 1 – variable– * 2 ;

    or such?
    a = variable– + 1 + –variable * 2 ;

    or at least, a?
    a = –variable + 1 + –variable * 2 ;

    gee… gee…

  4. First the multiplication is

    int a = 1;
    int b;
    First the multiplication is + ++a;
    cout << b << endl;

    First the multiplication is 6, First the multiplication is

    First the multiplication is, how everything breaks:

    int a = 1;
    int b;
    First the multiplication is + ++a + ++a;
    cout << b << endl;

    how everything breaks 12, how everything breaks 10. how everything breaks – instead 20 how everything breaks 15

    1. может: int b=0;
      чтоб мусор не засасывал из памяти в виду отсутствия определённости

  5. according to the rules, first performed the multiplication : 2 * 2 = 4
    next : 2 + 1 + 4 = 7 .
    if correct answer 10 , then the prefix increment should
    take precedence over multiplication , but
    this is not mentioned in the article. .
    correct me or explain more specifically the solution of the problem in the order of execution of the signs !

Leave a Reply

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