Simple question really, although I'm not sure if there's an answer. I am handling an integer which can be positive or negative.
However, I would like to either increment or decrement it dependant on the sign.
For example, if it's "2", then add 1 and make it "3".
If it's "-2", then subtract 1 and make it "-3".
I already know the obvious method to fix this by adding if statements and having two separate increment and decrement sections. But, I'm trying to limit the amount of code I use and would like to know if there's a similar way of doing this from a built-in function or procedure.
Try it:
int IncOrDec(int arg)
{
return arg >= 0 ? ++arg : --arg;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With