PrimRock
PrimRock

Reputation: 1156

Incrementation not showing in MessageBox and variable values being changed

I have two questions about this code...

  1. Why is that at line 10 it starts keeping the current value. For example,

    int a = 7
    

    (a += 4) Which is 11 is brought over to the next line of code (a -= 4) now making it 7. Instead of just using it initial declaration for the variable a which is 7. How come I don't get 3 ? Is the = in the += operator changing what I initially declared it in the beginning of the code ? Does a still hold the value 7 in memory, or does those statements change that?

  2. At the last MessageBox.Show() statement. I increment a by 1 using a++. However, I get the same value I had for the previous MessageBox.Show(). How come it didn't increment ??

This is the code:

private void button1_Click(object sender, EventArgs e)
{
    int a = 7;
    int b = 3;

    MessageBox.Show((a + b).ToString(), "a+b");
    MessageBox.Show((a - b).ToString(), "a-b");
    MessageBox.Show((a * b).ToString(), "a*b");
    MessageBox.Show((a / b).ToString(), "a/b");
    MessageBox.Show((a += 4).ToString(), "a+=4"); //adds 4 to a
    MessageBox.Show((a -= 4).ToString(), "a-=4"); //substracts 4 from a
    MessageBox.Show((a *= 4).ToString(), "a*=4"); //multiplies 4 from a
    MessageBox.Show(a++.ToString(), "a++"); //adds 1 to a
}

Upvotes: 4

Views: 257

Answers (3)

Yuval Itzchakov
Yuval Itzchakov

Reputation: 149538

How come I don't get 3 ? Is the "=" in the "+=" operator changing what I initially declared it in the beginning of the code ?

The += operator is equivalent to:

a = a + 4

Effectively assigning a new value to a.

Does "a" still hold the value 7 in memory, or does those statements change that?

It doesn't. After your first assignment, it changes.

At the last MessageBox.Show() statement. I increment "a" by 1 using "a++". However, I get the same value I had for the previous MessageBox.Show(). How come it didn't increment ??

That's what happens when you use ++ as a postfix. The docs say:

The second form is a postfix increment operation. The result of the operation is the value of the operand before it has been incremented.

But, if you use it as a prefix:

MessageBox.Show((++a).ToString(), "++a");

You'll see the updated value, again as the docs say:

The first form is a prefix increment operation. The result of the operation is the value of the operand after it has been incremented.

Upvotes: 5

Pradnya Bolli
Pradnya Bolli

Reputation: 1943

 MessageBox.Show((a += 4).ToString(), "a+=4"); //adds 4 to a

This sentance result is 7+4=11 so in a it takes 11 value after that

MessageBox.Show((a -= 4).ToString(), "a-=4"); //substracts 4 from a

for this sentance it take a value i.e current value it 11 i.e 11-4=7 so a value now is 7;

MessageBox.Show(a++.ToString(), "a++"); //adds 1 to a

in this it post-increment condition so in this it use that value and for next loop it increment by 1 For Above condition you can use

MessageBox.Show((++a).ToString(), "a++"); //adds 1 to a

MessageBox.Show((a *= 4).ToString(), "a*=4"); //multiplies 4 from a

and for this also it take 7 and += mean

a=a+b;

for post incerement and pre incerement condition go through this What is the difference between pre increment and post increment operator

Upvotes: 3

Paolo Costa
Paolo Costa

Reputation: 1989

(a += 4) increments the value of a by 4 and returns the incremented value.

a++ increments the value of a by 1 but still returns the original value.

++a increments the value of a by 1 and returns the incremented value.

try to add this line and you will notice it:

MessageBox.Show((++a).ToString(), "a++"); 

Upvotes: 2

Related Questions