Incrementing and Decrementing - Just Trying to Understand
-
x = 10 y = 100 z = 1112 lightbulbs still on? My Coding Journey
I think you had a brief power cut...:laugh:
The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)
-
I think you had a brief power cut...:laugh:
The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)
my electrons, neutrons and protons all turned into morons My Coding Journey
-
:laugh: I like lightbulb moments! Try this:
int x = 10;
int y = 100;
int z = ++x + (y++ * x);
Console.WriteLine("x = {0}, y = {1}, z= {2}", x, y, z);If you can work that out in your head, you are doing very, very well! Normally, they don't get that complex - they are generally used for array indexes as such like:
byte[] data = File.ReadAllBytes(@"D:\Temp\MyFile.txt");
int i = 0;
do
{
if (data[i++] == 'x')
{
break;
}
} while (i < data.Length);The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)
z = 1012 ???:confused::confused::confused: My Coding Journey
-
my electrons, neutrons and protons all turned into morons My Coding Journey
Maybe one or two of them! :laugh:
The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)
-
Maybe one or two of them! :laugh:
The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)
Ah S4!T... *Siiiiigh* Let me look at it again… This time I will type it out so you can see how I get my answers. My Coding Journey
-
Maybe one or two of them! :laugh:
The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)
before I go look at it again… Did I get x and y right? Is it z, that I got wrong? My Coding Journey
-
z = 1012 ???:confused::confused::confused: My Coding Journey
Ok, let's look at it (though it's a PITA to work out, I admit) and substitute the values:
int x = 10;
int y = 100;
int z = ++x + (y++ * x);++x
means "add one tox
and use the new value", sox
becomes11
, and the calculation becomes
z = 11 + (y++ * x)
y++
means "Add one toy
and use the old value", soy
becomes101
, and the calculation becomes
z = 11 + (100 * x)
- We only have
x
left to worry about, so get the current value of it (which is 11 because we changed it in step 1) and the calculation becomes
z = 11 + (100 * 11)
Which is
z = 11 + 1100
Or
z = 1111
So the final result is:
x = 11, y = 101, z= 1111
This is a lot more complex than anything you should have to meet in "real life" (hence the discussion above about hitting people who do that kind of thing and why C++ will give you different results)
The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)
-
Ok, let's look at it (though it's a PITA to work out, I admit) and substitute the values:
int x = 10;
int y = 100;
int z = ++x + (y++ * x);++x
means "add one tox
and use the new value", sox
becomes11
, and the calculation becomes
z = 11 + (y++ * x)
y++
means "Add one toy
and use the old value", soy
becomes101
, and the calculation becomes
z = 11 + (100 * x)
- We only have
x
left to worry about, so get the current value of it (which is 11 because we changed it in step 1) and the calculation becomes
z = 11 + (100 * 11)
Which is
z = 11 + 1100
Or
z = 1111
So the final result is:
x = 11, y = 101, z= 1111
This is a lot more complex than anything you should have to meet in "real life" (hence the discussion above about hitting people who do that kind of thing and why C++ will give you different results)
The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)
okay, it is relieving I was only off by one My Coding Journey
-
okay, it is relieving I was only off by one My Coding Journey
Welcome to the "I hit people who do that" club - your laminated membership card is in the post... :laugh:
The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)
-
I know - BUT, why the hell would anyone write such a monstrosity. ;P Also, as it was clearly a beginner question, I was trying to simplify. So you get 10/10 for correctness but 2/10 for being clear for the sake of the OP.. :omg:
wait a minute? Who gets 10/10 for being correct? OG or Me? My Coding Journey