5 Replies - 280 Views - Last Post: 26 July 2012 - 10:04 AM Rate Topic: -----

#1 Phrax001  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 26-July 12

Very Very basic C# question.

Posted 26 July 2012 - 09:39 AM

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..

Is This A Good Question/Topic? 0
  • +

Replies To: Very Very basic C# question.

#2 Phrax001  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 26-July 12

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.
Was This Post Helpful? 0
  • +
  • -

#3 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 3872
  • View blog
  • Posts: 11,405
  • Joined: 18-April 07

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.

This post has been edited by Martyr2: 26 July 2012 - 09:49 AM

Was This Post Helpful? 0
  • +
  • -

#4 Curtis Rutland  Icon User is online

  • (╯°□°)╯︵ (~ .o.)~
  • member icon


Reputation: 3793
  • View blog
  • Posts: 6,390
  • Joined: 08-June 10

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.
Was This Post Helpful? 0
  • +
  • -

#5 Phrax001  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 26-July 12

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.
Was This Post Helpful? 0
  • +
  • -

#6 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1688
  • View blog
  • Posts: 2,553
  • Joined: 21-June 11

Re: Very Very basic C# question.

Posted 26 July 2012 - 10:04 AM

View PostPhrax001, on 26 July 2012 - 06:45 PM, said:

var1 = --var2;

means var1 = var2 - 1.


No, it means var1 = var2 = var2 - 1; or more readably:

var2 = var2 - 1;
var1 = var2;



In other words: The difference between var2 = --var1 and var2 = var1 - 1 is that in the former case, the value of var2 changes too.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1