Thread: What else could I use instead of this IF statement?

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    25

    What else could I use instead of this IF statement?

    I am just curious if there is any other way I could write this code, I see 3 if statements in a row and am wondering if there is a more efficient way.

    Code:
    if (number1<0)
    number1*=-1;
    if (number2<0)
    number2*=-1;
    if (number2>=number1)
    c=b;

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    That looks fine.

    It's clean and readable.

    Don't try to use weird obfuscated constructs to try to make it "faster", because, 1) it probably won't be, and 2) it will just make your code unreadable and unmaintainable.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    3 in a row is nothing... some programs have dozens.

    Unless there's something in common, such as differing values of the same variable. What you got is about as good as it gets.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    25
    I should have included this,
    Code:
    s=values[j]+values[l];
    number1=r-t;
    number2=r-s;
    if (number1<0)
    number1*=-1;
    if (number2<0)
    number2*=-1;
    if (number2>=number1)
    c=b;
    I tried changing it up to:
    Code:
    s=values[j]+values[l];
    number1 = abs(r-t);
    number2 = abs(r-s);
    if (number2>=number1)
    c=b;
    However it doesnt work, it says: implicit declaration of function 'abs'
    What is happening?
    Last edited by dsured; 02-01-2011 at 10:37 PM. Reason: forgot to include s=values[j]+values[l];

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You didn't include the header file that abs() needed. Not sure, but probably that is <math.h> that you need.

    Compile and link that, and see how it works then.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    25
    I did include math.h in the header.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    abs is in <stdlib.h>

  8. #8
    Registered User
    Join Date
    Dec 2010
    Posts
    31
    Quote Originally Posted by dsured View Post
    I did include math.h in the header.

    your mileage may vary but, if your using an unix environment try the following cli command:

    $ man abs
    ABS(3) NEWLIB

    NAME
    2.4 `abs'--integer absolute value (magnitude)

    SYNOPSIS
    #include <stdlib.h>
    int abs(int I);
    ...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. While Statement Issue.
    By mcertini in forum C++ Programming
    Replies: 6
    Last Post: 01-14-2011, 12:34 PM
  2. bizarre print statement glitch
    By spongefreddie in forum C Programming
    Replies: 4
    Last Post: 09-23-2010, 10:39 AM
  3. Replies: 3
    Last Post: 08-16-2010, 10:00 AM
  4. Usefulness of the "else if" statement
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 05:19 AM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 04:49 PM