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")
}
Author by
lab12
Updated on July 09, 2022Comments
-
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 almost 14 yearsUntil they discover functions like log(x).
-
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 almost 14 years@Deniz: Mathematicians say 0 is a non-negative value; but it's not positive.
-
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 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 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 almost 14 yearsLooks like i understood this "non-negative" word as "positive", so thanks for correction :)
-
Yogesh Patel almost 5 yearsWhat is signum() here .?
-
Sourabh Kumbhar almost 5 yearsIt is function which returns '-1' if this value is negative and '1' if it's positive otherwise, '0'.
-
Yogesh Patel almost 5 yearsis this native function or you did your self .?
-
Sourabh Kumbhar almost 5 yearsIt's inbuilt function
-
Yogesh Patel almost 5 yearscan you please in which type this support because I working in objective-c means it Float, Int, Decimal , NSNumber .?
-
Yogesh Patel almost 5 yearsvar 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 almost 5 yearsFind here so help you developer.apple.com/documentation/swift/int64/2886319-signum