Even for me this is a basic question but I'm getting confused while going through notes. I'm learning C# from a book I brought a few weeks back.
It says:
Unary:
Var1 = -Var2; ->> Var1 is assigned the value of Var2 multiplied by -1.
Then it says on the next page:
Var1 = --Var2; ->> Var1 is assigned the value of Var2 -1. Var2 is decremented by 1.
This seems odd to me. Surely it should mean Var1 is assigned the value of var2 multiplied by -1 as well as decremented by 1. Why does it change from multiplying Var2 by -1 to just taking 1 away from the value of Var2?
Think I understand it now actually. Seems more clear after rereading it all. Having a head ache probably doesn't help..
Very Very basic C# question.
Page 1 of 15 Replies - 280 Views - Last Post: 26 July 2012 - 10:04 AM
Replies To: Very Very basic C# question.
#2
Re: Very Very basic C# question.
Posted 26 July 2012 - 09:45 AM
So basically having 2 (-)'s changes it to decreasing var2 by 1.
so Var1 = -var2;
= Var1 = -(6) if var2 was 6 for example and:
var1 = --var2;
means var1 = var2 - 1.
Thanks for reading anyway.
so Var1 = -var2;
= Var1 = -(6) if var2 was 6 for example and:
var1 = --var2;
means var1 = var2 - 1.
Thanks for reading anyway.
#3
Re: Very Very basic C# question.
Posted 26 July 2012 - 09:47 AM
No "-" is one operator and "--" is another operator.
One is to essentially flip the sign and one is to decrement. Think of the "--" as its own operator, it is not two "-".
Hope that helps
Edit: -- and ++ is just shorthand for x = x - 1 and x = x + 1. Now using them before or after the term is referred to as pre and post incrementing/decrementing. You should be reading about that in your book as well and the difference.
One is to essentially flip the sign and one is to decrement. Think of the "--" as its own operator, it is not two "-".
Hope that helps
Edit: -- and ++ is just shorthand for x = x - 1 and x = x + 1. Now using them before or after the term is referred to as pre and post incrementing/decrementing. You should be reading about that in your book as well and the difference.
This post has been edited by Martyr2: 26 July 2012 - 09:49 AM
#4
Re: Very Very basic C# question.
Posted 26 July 2012 - 09:49 AM
Just to clear things up, - and -- are completely different operators. They behave in completely different ways. The decrement operator is not two negation operators. It's one symbol made up of two characters, and it is not related to the other in any way.
#5
Re: Very Very basic C# question.
Posted 26 July 2012 - 10:03 AM
Thanks for helping me out. The way you describe it to me makes perfect sense and know clearly understand it. Just the book didn't explain it very well.
#6
Re: Very Very basic C# question.
Posted 26 July 2012 - 10:04 AM
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote





|