-1

I'm having a hard time understanding why using a increment operator in a for loop in C++ has a different result than doing 'variable' + 1. The variable in the second case simply isn't remembered after each iteration of the loop:

Take the following code:

#include <iostream>

int main(){
    int a{0};
    for (int i = 0; i < 5; i++){
        std::cout << ++a;
    }
return 0;
}

It outputs as expected: 12345

However, if I instead replace ++a with a + 1:

#include <iostream>

int main(){
    int a{0};
    for (int i = 0; i < 5; i++){
        std::cout << a + 1;
    }
return 0;
}

I get: 11111

Even if I make the 'a' variable static:

static int a{0};

It still outputs 11111.

Why doesn't 'a + 1' preserve its value after every loop? What would I need to do if I wanted to preserve its value after every iteration without using ++ (for instance, with another operation like a * 2)?

Guilherme
  • 17
  • 3
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Jason Oct 26 '22 at 07:55
  • 1
    `std::cout << a + 1` doesn't affect `a` in any way. This is explained in any [beginner c++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – Jason Oct 26 '22 at 07:55
  • 2
    For the same reason that `5+6` doesn't morph `5` into `11`. – Quimby Oct 26 '22 at 07:56
  • `std::cout << a; a += 1;` (or `a *= 2;` or whatever it is that you want to do). – Michael Oct 26 '22 at 07:56
  • @GuilhermeMS No, it does not affect a. The `a` is still `0` after doing `a+1`. – Jason Oct 26 '22 at 07:59
  • **Stackoverflow is not an introduction to C++**. This is explained in any [beginner c++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Jason Oct 26 '22 at 08:02
  • I still don't quite understand what your end-goal is. It is obvious why `a + 1` doesn't do anything to `a`, since there is no assignment to `a` going on. – PaulMcKenzie Oct 26 '22 at 08:03
  • Interesting. I get it now @JasonLiam, thanks a lot. With a + 1 'a' was never assigned anything. It got incremented by 1 in the output because the result of that operation was being passed to cout, however a was still 0, that's why when it looped it simply added 0 + 1 again. :D – Guilherme Oct 26 '22 at 08:08
  • Since `a` is an `int`, the expression `a + 1` accesses the value of `a`, and produces a result equivalent to `a + 1`. It does not change the value of `a` at all. This is explained in any beginner/introductory text on C++ (and also in any beginner/introductory text on C - the working of the expression `a + 1` is the same in both C and C++, although C does not have `std::cout`). – Peter Oct 26 '22 at 10:13

4 Answers4

1

++a is equivalent to the assignment a= a + 1 (or a+= 1). The expression a + 1 alone does not modify a.

C++ will allow you to write std::cout << (a= a + 1);.

1

Why doesn't 'a + 1' preserve its value after every loop?

a + 1 is an expression that doesn't assign anything to a. That is a is not affected in any way. This means when you do just a + 1, the value of a is still 0(the old value).

On the other hand ++a has the same effect as:

  v---------->assignment done here implicitly
a = a+1

In the above expression ++a, the value of is incremented by 1 so that the new value of a is now 1.

Jason
  • 36,170
  • 5
  • 26
  • 60
0

a++ is not the same thing as a+1 but as a=a+1.
What's the difference?

Well:
a+1 contains the value, which is one higher than the value of a.
a=a+1 means that, in top of this, this value gets assigned to the variable a, which means something like "replace a by its value plus one".

This gives following possibilities (I also show the difference between a++ and ++a):

int a=3;
cout<<a+1; // output : 4. Value of 'a' equals 3.
cout<<a+1; // output : 4. Value of 'a' equals 3.
cout<<a+1; // output : 4. Value of 'a' equals 3.

int a=3;
cout<<a++;  // output : 3. Value of 'a' becomes 4 after having shown it on screen.
cout<<a++;  // output : 4. Value of 'a' becomes 5 after having shown it on screen.
cout<<a++;  // output : 5. Value of 'a' becomes 6 after having shown it on screen.

int a=3;
cout<<++a;  // output : 4. Value of 'a' becomes 4 before showning it on screen.
cout<<++a;  // output : 5. Value of 'a' becomes 5 before showning it on screen.
cout<<++a;  // output : 6. Value of 'a' becomes 6 before showning it on screen.
Dominique
  • 16,450
  • 15
  • 56
  • 112
0

Conceptually, the built-in arithmetic operators like + and * evaluate their operands, perform the respective operation with the thusly obtained values, and create a temporary object that contains the result.

The operands, one of which is your a, are only read from, not written to. That is more obvious when you consider that + is commutative, that is, its operands can be swapped without affecting the result: a+1 is by definition the same as 1+a, and clearly you cannot write anything back to the immediate value 1.

Generally, as you certainly know, = is used in C and C++ to write a value to a variable. C, and by means of ancestry also C++, provide shortcuts to write back the result of certain operations to one of their operands: it's the combination of the operator and the assignment =, for example a += 1. Like all assignments, this one is an expression (that is, it has a value) which has the value of the variable after the operation and assignment. Like all other expressions, you can use it as a subexpression, even though that's not very common: cout << (a += 1); would achieve the desired effect (the parentheses are necessary because assignment has about the lowest operator precedence). Because incrementing (and decrementing) by one is so very common, C and C++ have a shortcut for the shortcut: ++a is the same as a+=1, so that one would typically write cout << ++a; (as a side effect obviating the need for parentheses).

Peter - Reinstate Monica
  • 15,048
  • 4
  • 37
  • 62