conditionals

This is a discussion on conditionals within the C Programming forums, part of the General Programming Boards category; Hi all, I've got a question about conditionals which I couldn't find anything about it on previous posts... I need ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    55

    conditionals

    Hi all,
    I've got a question about conditionals which I couldn't find anything about it on previous posts...
    I need to display 4 numbers when these are in range between min and max. I basically want to evaluate (true or false) in an "if" statement:
    Code:
    (min<=No1) && (max>+No1) && (min<=No2) && (max>+No2) &&(min<=No3) && (max>=No3) && (min<=No4) && (max>=No4)
    I want to make this statement shorter but can you please explain why this is not working:?
    Code:
    (min<=No1<=max) && (min<=No2<=max) && (min<=No3<=max) && (min<=No4<=max)
    Thanks!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,632
    min<=No1 evaluates to a boolean value, i.e., either 0 or 1.

    Therefore, min <= No1 <= max is either:
    0 <= max
    or
    1 <= max

    This is clearly different from No1 <= max.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    becuae
    Code:
    min<=No1<=max
    means: (min <= No1) <= max - which doesn't do the same thing that you think it does, since the result of the "min<=No1" is either 0 or 1, which is likely outside the range of No1 .. max.

    You could write a function or use a macro to do it:
    Code:
    int within(int x, int min, int max)
    {
       return (x >= min) && (x <= max);
    }
    or
    Code:
    #define WITHIN(x, min, max) (((x) >= (min)) && ((x) <= (max)))
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Nov 2004
    Posts
    55
    this makes a lot of sense. Thank you very much!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Type definitions within conditionals?
    By mushu in forum C++ Programming
    Replies: 4
    Last Post: 06-11-2006, 04:16 PM
  2. How to eliminate irrelevant conditionals?
    By cdave in forum C Programming
    Replies: 9
    Last Post: 12-10-2005, 03:39 PM
  3. circle and conditionals
    By a1pro in forum C++ Programming
    Replies: 7
    Last Post: 04-27-2005, 02:05 AM
  4. Conditionals
    By uniqueniq in forum C Programming
    Replies: 6
    Last Post: 02-13-2003, 05:20 PM
  5. Raycasting question
    By VirtualAce in forum Game Programming
    Replies: 10
    Last Post: 12-10-2001, 04:08 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21