I still don't understand how pre and post decrement operators work

I am currently going through lecture 62 in the Bull Cow game section of the Unreal Engine C++ developer course. I am completely new to coding and can’t seem to wrap my head around some of the concepts being introduced in this section.

During the lecture, there are 2 integers being declared like so:

int32 a = 1;
int32 b = a++;

We then plug them into the following function:

PrintLine(TEXT(“%i , %i,”), a, b);

And when we run the Bull Cow game (around the 4 minute mark of the video), we get the values of 2, 1 printed within the game.

So my question is why does this happen?

More specifically, why does the declaration of the second integer appear to override the declaration of the first?

Why is it not 1, 1 instead? (Since we said int32 a = 1)

In other words, why does a become 2?

And what exactly is it within our function that makes it 2 when it gets printed within the Bull Cow game?

I’m confused. Please give me a very dumbed down kindergarten level explanation as to what is going on here.

Postfix increment increments and returns the old value.

int32 a = 1;

Here a is declared an initialised to 1.

a++

This would increment a to 2 and then return its old value of 1 which is then used to initialise b

Thank you for the reply, Dan.

But I still don’t understand why a gets incremented to 2, which is then used to initialize b. In other words, why doesn’t the old value of a (i.e. 1) get used instead of the new value 2?

Let me try to put it another way.

In the same video lecture that I previously referenced, we go on to declare more integers like so:

int32 a = 1;
int32 b = ++a;
int32 c = ++ ++a;

Our function then becomes:

(TEXT(%i , %i, %i), a, b, c);

And when we run the Bull Cow game (around the 5:39 mark of the video), we get 4, 2, 4.

Then we keep declaring more and more integers until we end up with

int32 a = 1;
int32 b = ++a;
int32 c = ++ ++a;
int32 d = a +=2
int32 e = a++;

And our function becomes:

(TEXT(%i , %i, %i, %i, %i), a, b, c, d, e);

And when we run the Bull Cow game this time around (around the 9:15 mark of the video), we get 7, 2, 4, 6, 6.

As I understand it, the first number there represents a.

And while I understand the mathematics behind why the number changed, I do not understand why the value of a would get listed as such. In other words, I understand that a “became” 7 in the end because we post incremented the value of e, which got posted as 6, but was due to change to 7 later.

But what I don’t understand is why does a keep changing?

Why was it 2, then 4, and then eventually 7?

Why was it not listed as 1 throughout the process? Why does it keep changing with every new integer that we declare?

So in the examples I just listed, why was it not 1, 2, 4 and 1, 2, 4, 6, 6 instead?

I hope my question makes sense.

Please forgive me if my logic here seems kind of dumb. lol

I’m just confused and want to clear things up in my head.

I’m not sure how to answer that other than “that’s what that operator does”.

a++

Increments a and returns the old value. In psuedo code

Save current value of a
a = a + 1
return saved value

For prefix increment it will increment and return the new value, again in pseudo code

a = a + 1
return a

Thanks again for your reply, Dan. Although it still seems a bit counterintuitive to me, I suppose I’ll just have to accept that the operator does what it does.

Maybe it’ll be better if you describe what you think ++a or a++ should do?

I don’t think that’ll help. I understand what ++a and a++ do. That’s not my issue.

I just don’t understand why that operator does what it does.

According to my logic, if we were to get 4, 2, 4 as a result, the function that would represent this would be written as (TEXT(%i , %i, %i), c, b, c).

When we write this: (TEXT(%i , %i, %i, %i, %i), a, b, c); I expect the result to be 1, 2, 4.

So why is it 4, 2, 4 and not 1, 2, 4? Since, by definition, a is equal to 1?

Why would c by 4 with how you think it works?

But you’ve been modifying a. The main purpose of the operator is to increment the variable by one. a is not constant.

Yes, that is my point of confusion right there. I don’t understand why a is not constant. I guess I was under the false impression that it should be.

So in my previous example, c would be 4 because it was modified. Meanwhile a would still be 1 since, according to my thinking, it was not modified.

Anyways, I think I understand the logic here now and that’s all that ultimately matters. The matter appears to be resolved.

Thank you again for your help!

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.