|
Worse. With goto at least only one thing can happen, even if it's not always immediately clear what that thing is.
|
|
|
|
|
I have seen some pretty nasty goto's in my life, but I have to agree that this seems to take the cake. Why would anybody want to use such a feature anyway, it seems daft.
|
|
|
|
|
I see you guys have been busy bees about this… I had to take a break, steam was coming from my ears because of this.
Anyway, I looked over the example in the exercise they gave me.
Give me about 30 min., I'll explain to you guys but I ended up getting the second expression in my exercise wrong.
By the way, thank you for trying to help clear this up for me.
Rob
<a href="http://www.widmarkrob.com">My Coding Journey</a>
|
|
|
|
|
Hi,
Sometimes a simple description can be very hard to grasp - especially if the simple description tries to abstract the details away.
In such cases I find it rewarding simply to dig a bit deeper...
Here goes the details:
.entrypoint
.maxstack 3
.locals init ([0] int32 x,
[1] int32 y,
[2] int32 z)
IL_0000: nop
IL_0001: ldc.i4.s 10
IL_0003: stloc.0
IL_0004: ldc.i4.s 100
IL_0006: stloc.1
IL_0007: ldloc.1
IL_0008: dup
IL_0009: ldc.i4.1
IL_000a: sub
IL_000b: stloc.1
IL_000c: ldloc.0
IL_000d: add
IL_000e: stloc.2
If instruction, stack and arithmic unit are alien terms to you, then above might be a bit tough. It is a window into a lower layer of code. The C# compiler outputs this in binary form as your assembly/executable. IL means Intermediate Language and it can be executed by a Virtual Machine aka a program. The idea is to simulate the arithmic unit of a cpu, so above code is also a window into history.
-- and ++ are special language features that comes nearly for free because of the (virtual) machine architecture.
I produced above from your code example by running IL Disassembly from Microsft .Net.
Kind Regards,
Keld Ølykke
|
|
|
|
|
Okay, just to recap… I'm going to show the example they gave me first… Then tell you how I understand what you guys have been trying to help me with.
Then I will post my exercise towards the end and try to explain how I came up with my answer doing it in my head before running the program and printing it to the console window.
<pre lang="c#">
// Example 3-2.cs
// Increment and Decrement
using System;
class ArithmeticOperators
{
public static void Main()
{
int x = 10;
int y = 100;
int z = y-- + x;
Console.WriteLine(z); // result = 110
Console.WriteLine(y); // result = 99 — The value of y after
// decrementing
z = --z + x;
Console.WriteLine(z); // result = 119
}
}
</pre>
So, we print out the variable z… Like it says, the result is 110. Okay, easy enough.
Just in my mind, the variable y automagically becomes 99.
Anyway, you guys have explained to me that the variable y takes on the new value after evaluating the expression.
Semi-sort of easy enough.
Using this as somewhat of a template for my exercise is probably what threw me off.
In the exercise, I got the first expression done in my head correctly. Meaning it matched what the console window printed out.
The second expression, using the example above.
I also thought the variable y would become the new value after evaluating the first expression.
That's why in my head, I came up with 121… The console window printed out 111.
Drill 3-1
Start with the following assignments:
int x = 10;
int y = 100;
int z = y;
Then write a C# program to compute and display the values of the
variables y and z after executing these expressions:
y = y++ + x;
z = ++z + x;
<a href="http://www.widmarkrob.com">My Online Journey</a>
|
|
|
|
|
WidmarkRob wrote: int z = y-- + x;
WidmarkRob wrote: Anyway, you guys have explained to me that the variable y takes on the new value after evaluating the expression.
y takes on the new value of y , which is sort of tautological..
One is subtracted from y , that is all.
|
|
|
|
|
Yes, I get that now…
Post-fix decrementing…
It takes on the new value after evaluating the expression.
On to my new problem.
I used this same type of thinking with my so-called simple exercise I was given.
y = y++ + x;
z = ++z + x;
In using that same thought process of y taking on the new value after evaluating the expression, in this new problem (the first expression).
I did this in my head before printing it to the console window and came up with 121.
The console window printed 111.
In trying to keep things in semi-sort of uniform fashion.
In my head, I also thought this variable y would take on the new value as well.
Where did I go wrong in my head?
<a href="http://www.widmarkrob.com">My Coding Journey</a>
|
|
|
|
|
|
int x = 10;
int y = 100;
int z = y;
Then write a C# program to compute and display the values of the
variables y and z after executing these expressions:
y = y++ + x;
z = ++z + x;
This exercise is virtually the same as the example they gave to me in the beginning. (My original question I was asking about)
When I print the variable y to the console window, the result is 110. (Which is what I came up but in my head)
Using the same thought process as in the example, where this variable y also takes on the new value (110)…
I used this new value in my head.
This so-called simple exercise is using "Boxing"? (Just throwing in another term that I learned, hopefully using it correctly)
the variable z now equals the variable y. (Which in my mind, I thought would equal 110)
The second expression.
Prefixes incrementing.
So I thought, 110+1+10.
That's how I came up with 121 in my head.
When I went to go print the variable z to the console window, the result was 111.
<a href="http://www.widmarkrob.com">My Coding Journey</a>
|
|
|
|
|
It's pretty simple:
int x = 10;
int y = 100;
int z = y;
At the end of this, you have three variables, each with separate values:
x y z
10 100 100 Although "y" and "z" contain the same number, they aren't linked together, so changing one does not change the other. Think of them as three pockets: two in your trousers, and one in your shirt. If you put ten coins in each pocket, they all have the same number. If you then take 5 coins out of your shirt pocket, you still have ten coins in each of your trouser pockets.
y = y++ + x; Equates to:
int y2 = y;
y = y + 1;
y = y2 + x;
Which means that "y" ends up holding 110 - as you have seen.
z = ++z + x; Is the equivalent of:
int z2 = z + 1;
z = z2 + x; Which is the same as saying:
z = 11 + 100; Which gives you the answer: 111
The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)
|
|
|
|
|
where it confuses me, if you look at the example I posted… The variable y takes on a new value after evaluating the expression.
In my head, I was applying that same logic/thinking too my exercise.
I also thought, in my exercise… That the variable y would also take on the new value (in this case, 110) after evaluating the first expression.
<a href="http://www.widmarkrob.com">My Coding Journey</a>
|
|
|
|
|
It does. But since you don't use "y" in the second expression that doesn't have any effect. Changing the value in "y" doesn't affect the value in "x" or "z" - it's like they are separate pockets. Adding coins to your shirt pocket doesn't affect the number of coins in either of your trouser pockets!
So:
int x = 10;
int y = 100;
int z = y;
y = y++ + x;
z = ++z + x;
The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)
|
|
|
|
|
Okay, I get what you're saying about the different pockets.
This variable y in this exercise is clearly not the same as the variable y in the example.
In the example: y takes on the new value after evaluating the expression. Okay, I understand (sort of)
In the exercise: y does not take on a new value after evaluating the first expression. Different pockets and something like that. Okay, I guess my question is:
When it is postfix, the variable takes on the new value?
When it is prefix, the variable does not take on the new value?
<a href="http://www.widmarkrob.com">My Coding Journey</a>
|
|
|
|
|
No, no - sorry, I'm probably confusing you. Let's go right back to the beginning.
Assume we have a function with just a two lines of code:
int x = 10;
Console.WriteLine(x);
If we run it, we obviously get "10" printed. So let's start with that and add bits between the two lines.
int x = 10;
x++;
Console.WriteLine(x); Will print 11, because the post increment adds one to the value of x
int x = 10;
++x;
Console.WriteLine(x); Will also print 11, because the pre increment adds one to the value as well.
Prefix and postfix operations always affect the value of the variable; the only difference is when the variable is altered.
With prefix, the variable is altered before it's value is used: So
int x= 10;
int y = ++x;
Console.WriteLine("x={0}, y = {1}", x, y); will give us "x = 11, y = 11" because x was altered before it's value was used in the expression. This is the equivalent of writing:
int x = 10;
x = x + 1;
int y = ++x;
Console.WriteLine("x={0}, y = {1}", x, y); With post fix, the variable is altered after its's value is used in the expression:
int x = 10;
int y = x++;
Console.WriteLine("x={0}, y = {1}", x, y); Will give us "x=11, y = 10" and is the equivalent of writing:
int x= 10;
int y = x;
x = x + 1;
Console.WriteLine("x={0}, y = {1}", x, y);
Does that make sense so far?
The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)
|
|
|
|
|
Yes, it is all a little confusing… While I was reading what you are trying to explain. I was trying to do it in my head before going on to reading the next few lines.
The postfix I ended up getting them backwards in my head.
x=10
and
y=11
you said the variable is altered after its value is used.
I think I'm getting confused on exactly which variable.
x is the variable with the postfix attached to it, so… That is the one I'm thinking that it's altered.
When in fact it is the variable y that is assigned to the variable x that is being altered.
<a href="http://www.widmarkrob.com">My Coding Journey</a>
|
|
|
|
|
The pre and post fix operations always affect the variable they are attached to: x++ or ++x will both increment the value of x by one, and they on there own will not affect the value of any other variables.
So when you use it in an expression:
int x = 10;
int y = ++x; the prefix op affects x only. y gets changed only because it is where the result of the expression on the RHS of the equals sign is stored when it is complete.
This is kinda difficult to explain without diagrams or being able to see your eyes glaze over...
Let's try this:
int x = 10;
int y = x++ + 5; This is evaluated as:
1) Set x equal to 10
2) Process the RHS of the line:
2.1) Get the value of x and save it for later
2.2) Incrmement x by one (so x is now 11)
2.3) Use the value you saved in 2.1 (10) and add 5 to it -> 15 save this as the result
3) Save the result of step 2 (which was 15) into y.
So you end up with x = 11, and y = 15
Prefix is the same process:
int x = 10;
int y = ++x + 5; This is evaluated as:
1) Set x equal to 10
2) Process the RHS of the line:
2.1) Incrmement x by one (so x is now 11)
2.2) Get the current value of x (11) and add 5 to it -> 16 save this as the result
3) Save the result of step 2 (which was 16) into y.
So pre- and post- fix ops affect the variable they are attached to, but when they affect them changes.
OK so far?
The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)
|
|
|
|
|
my eyes glaze over long time ago, every time I open my book on C#… The clear mucus has to be removed with concrete trowel.
"The ++ and -- operators also support prefix notation, as described in Section 7.6.5. The result of x++ or x-- is the value of x before the operation, whereas the result of ++x or --x is the value of x after the operation. In either case, x itself has the same value after the operation."
<a href="http://msdn.microsoft.com/en-us/library/aa691363(v=vs.71).aspx">http://msdn.microsoft.com/en-us/library/aa691363(v=vs.71).aspx</a>[<a href="http://msdn.microsoft.com/en-us/library/aa691363(v=vs.71).aspx" target="_blank" title="New Window">^</a>
"In either case, x itself has the same value after the operation."
x has the same value, after were done evaluating the expression that it is in?
Did I understand that right?
In my exercise:
Start with the following assignments:
int x = 10;
int y = 100;
int z = y;
Then write a C# program to compute and display the values of the
variables y and z after executing these expressions:
y = y++ + x;
z = ++z + x;
So, now the variable y never really changes in the first expression. Using what MSDN described in their article, now the variable y returns to its original value of 100 even though it never really changed in the first place for the first expression. Right?
Now the second expression makes sense to me now on why it would be 111.
<a href="http://www.widmarkrob.com">My Coding Journey</a>
|
|
|
|
|
WidmarkRob wrote: Did I understand that right?
No, it's a bit badly explained: it isn't referring to the whole expression, just the little "x++" or "++x" bit - it doesn't matter if you do prefix or postfix, the value of x is the same after them both. That doesn't mean it is unchanged, just that it doesn't matter which you do, they both affect the variable in the same way.
y = ++x + 10;
y = x++ + 10;
What it's saying is that if x started at 100, in both cases it will be 101 afterward, despite y getting a different value each time.
WidmarkRob wrote: So, now the variable y never really changes in the first expression.
y does get changed by the postfix increment, it's just that the changed value is overwritten with the total from the whole expression.
The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)
|
|
|
|
|
Aaaahhhh... maybe I get it now, maybe…
Give me a simple exercise… Maybe just barely, a little bit tiny bit harder than what I've been working with to see if I understand right.
I'll try to do it in my head really quick before I go and open Visual C# Express.
Your last line:
"y does get changed by the postfix increment, it's just that the changed value is overwritten with the total from the whole expression."
Kind of sort of little a lightbulb in my head.
D'oh
<a href="http://www.widmarkrob.com">My Coding Journey</a>
|
|
|
|
|
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)
|
|
|
|
|
x = 10
y = 100
z = 1112
lightbulbs still on?
<a href="http://www.widmarkrob.com">My Coding Journey</a>
|
|
|
|
|
I think you had a brief power cut...
The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)
|
|
|
|
|
my electrons, neutrons and protons all turned into morons
<a href="http://www.widmarkrob.com">My Coding Journey</a>
|
|
|
|
|
Maybe one or two of them!
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.
<a href="http://www.widmarkrob.com">My Coding Journey</a>
|
|
|
|
|