123
-=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- (c) WidthPadding Industries 1987 0|567|0 -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=-
Socoder -> Off Topic -> X-- vs X>0

Tue, 17 Jan 2012, 12:35
Afr0
Is while(x--) in C equal to while(x > 0) in C#?
Tue, 17 Jan 2012, 12:35
HoboBen
Only if x is positive/unsigned. If x starts at -1, x-- is true (because -2 is true, as it is non-zero). If you can guarantee x is never negative, then yes.

Aside: I can never remember the order in which x-- is done; does it return the decremented value, or the value before it was decremented? In which case, if x is initially 0 it may have a different effect. It's best to be explicit with the conditional you suggested.

-=-=-
blog | work | code | more code
Tue, 17 Jan 2012, 13:11
Afr0
I ended up porting it (and all other implicit boolean statements) as such:



-=-=-
Afr0 Games

Project Dollhouse on Github - Please fork!
Tue, 17 Jan 2012, 16:13
CodersRule
HoboBen Aside: I can never remember the order in which x-- is done; does it return the decremented value, or the value before it was decremented?


I don't remember which language this was in, but if I remember correctly... x-- returns the value, THEN decrements it. --x decrements the value, THEN returns it.


Tue, 17 Jan 2012, 18:44
Evil Roy Ferguso
Well, that's not entirely an accurate conversion.

If x starts at 0,



will not execute, but



WILL execute once.

Convert.ToBoolean on ints isn't really "idiomatic" C#; I'd suggest just going with the following if there's no better looping construct:


Tue, 17 Jan 2012, 23:46
Afr0
Thanks!