Check if Integer is Positive or Negative - Objective C

32,293

Solution 1

if (x >= 0)
{
    // do positive stuff
}
else
{
    // do negative stuff
}

If you want to treat the x == 0 case separately (since 0 is neither positive nor negative), then you can do it like this:

if (x > 0)
{
    // do positive stuff
}
else if (x == 0)
{
    // do zero stuff
}
else
{
    // do negative stuff
}

Solution 2

Maybe I am missing something and I don't understand the quesiton but isn't this just

if(value >= 0)
{
}
else
{
}

Solution 3

-(void) tellTheSign:(int)aNumber
{
   printf("The number is zero!\n");
   int test = 1/aNumber;
   printf("No wait... it is positive!\n");
   int test2 = 1/(aNumber - abs(aNumber));
   printf("Sorry again, it is negative!\n");
}

;-)

Seriously though, just use

if (x < 0) {
// ...
} else if (x == 0) {
// ...
} else {
// ...
}

Don't overdo methods ans properties and helper functions for trivial things.

Solution 4

In Swift

var value = 5
if value.signum() == 1 {
   print("Positive value")
} else if value.signum() == -1 {
   print("Negative value")
} else if value.signum() == 0 {
   print("Zero value")
}
Share:
32,293
lab12
Author by

lab12

Updated on July 09, 2022

Comments

  • lab12
    lab12 almost 2 years

    How can I tell in objective-c coding if an integer is positive or negative. I'm doing this so that I can write an "if" statement stating that if this integer is positive then do this, and if its negative do this.

    Thanks,

    Kevin

  • Eiko
    Eiko almost 14 years
    Until they discover functions like log(x).
  • Paul R
    Paul R almost 14 years
    @BlueRaja - Danny Pflughoeft: well it doesn't have a - in front of if, so that's good enough for me. ;-)
  • BlueRaja - Danny Pflughoeft
    BlueRaja - Danny Pflughoeft almost 14 years
    @Deniz: Mathematicians say 0 is a non-negative value; but it's not positive.
  • Deniz Acay
    Deniz Acay almost 14 years
    @BlueRaja - Danny Pflughoeft: if...else statemens evaluate logical expressions as true/false, so for computer "if it is not negative, it is positive".
  • Eiko
    Eiko almost 14 years
    @Deniz Acay: Then please do a if(-1) printf("Wow I'm positive"); and fasten your seatbelts.... C takes everything != 0 as true.
  • JWWalker
    JWWalker almost 14 years
    @Deniz Acay: I've never heard of mathematicians accepting 0 as positive (and I used to be a math professor). Mathematicians use the word "nonnegative" all the time. They wouldn't need the word if it meant the same thing as "negative".
  • Deniz Acay
    Deniz Acay almost 14 years
    Looks like i understood this "non-negative" word as "positive", so thanks for correction :)
  • Yogesh Patel
    Yogesh Patel almost 5 years
    What is signum() here .?
  • Sourabh Kumbhar
    Sourabh Kumbhar almost 5 years
    It is function which returns '-1' if this value is negative and '1' if it's positive otherwise, '0'.
  • Yogesh Patel
    Yogesh Patel almost 5 years
    is this native function or you did your self .?
  • Sourabh Kumbhar
    Sourabh Kumbhar almost 5 years
    It's inbuilt function
  • Yogesh Patel
    Yogesh Patel almost 5 years
    can you please in which type this support because I working in objective-c means it Float, Int, Decimal , NSNumber .?
  • Yogesh Patel
    Yogesh Patel almost 5 years
    var direct we can you in swift but objc we need to specify the type first hot you understand that please tell me so I can do this thing Thank You :)
  • Sourabh Kumbhar
    Sourabh Kumbhar almost 5 years