The increment is not displayed in MessageBox and variable values ​​of variables

I have two questions about this code ...

  • Why does line 10 start saving the current value. For instance,

    int a = 7
    

    (a += 4)This 11wraps to the next line of code (a -= 4), now does it 7. Instead of just using this initial declaration for a variable athat is 7. Why am I not getting 3? Is the =statement a +=change to what I originally declared at the beginning of the code? Does the avalue remain 7in memory, or are these statements changing?

  • In the last expression MessageBox.Show(). I increase aby 1using a++. However, I get the same value as the previous one MessageBox.Show(). Why hasn't it increased?

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
}
+4
source share
3 answers

Why am I not getting 3? Is the "=" in the "+ =" operator a change I first declared this at the beginning of the code?

The operator is +=equivalent to:

a = a + 4

Effectively assigning a new value a.

Does the value "a" store the value 7 in memory, or do these statements change it?

This is not true. After your first assignment, it changes.

MessageBox.Show(). "a" 1, "A ++". , , MessageBox.Show(). ?

, ++ . :

- . , .

:

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

, , :

- . , .

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

7 + 4 = 11, 11

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

i.e it 11 11-4 = 7, 7;

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

post-increment, , 1

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

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

7 +=

a=a+b;

post incerement pre incerement pre increment post increment

+3

(a + = 4) a 4 .

a ++ a 1, .

++ a a 1 .

, :

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

All Articles