x = x < y < 2; WHY IS THIS ALWAYS TRUE!?

This is a discussion on x = x < y < 2; WHY IS THIS ALWAYS TRUE!? within the C Programming forums, part of the General Programming Boards category; sorry, this is a newbie question, but why is this expression always true? For example: x = 3; y = ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    36

    x = x < y < 2; WHY IS THIS ALWAYS TRUE!?

    sorry, this is a newbie question, but why is this expression always true?

    For example:

    x = 3; y = 4; x = x < y < 2;

    x = 3 < 4 < 2;

    3 < 4 = T
    4 < 2 = F

    therefore T < 2;

    is this true because 2 is a non-zero, therefore evaluates to T?

    please help!

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    The expression
    Code:
    x = x < y < 2;
    is equivalent to
    Code:
    int less = x < y;
    x = less < 2;
    or
    Code:
    int less;
    if(x < y) less = 1;
    else less = 0;
    
    if(less < 2) x = 1;
    else x = 0;
    Generally, you don't use that sort of syntax. Maybe you're looking for
    Code:
    x = x < y && y < 2;
    which is the same as
    Code:
    if(x < y && y < 2) x = 1;
    else x = 0;
    where && means "and".

    There's some information about boolean logic on this page: http://www.cprogramming.com/tutorial/c/lesson2.html
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    it's not the case that 3 < 4 = T. It's the case that 3 < 4 == 1. The comparison operators return 1 or 0.

    So x = 3 < 4 < 2 simplifies to x = (3 < 4) < 2 which simplifies to 1 < 2 which simplifies to 1.
    There are 10 types of people in this world, those who cringed when reading the beginning of this sentence and those who salivated to how superior they are for understanding something as simple as binary.

  4. #4
    Registered User
    Join Date
    Jun 2007
    Posts
    36
    Quote Originally Posted by dwks View Post
    The expression
    Code:
    x = x < y < 2;
    is equivalent to
    Code:
    int less = x < y;
    x = less < 2;
    or
    Code:
    int less;
    if(x < y) less = 1;
    else less = 0;
    
    if(less < 2) x = 1;
    else x = 0;
    Generally, you don't use that sort of syntax. Maybe you're looking for
    Code:
    x = x < y && y < 2;
    which is the same as
    Code:
    if(x < y && y < 2) x = 1;
    else x = 0;
    where && means "and".

    There's some information about boolean logic on this page: http://www.cprogramming.com/tutorial/c/lesson2.html
    thanks for the link, ill keep that handy!

  5. #5
    Registered User
    Join Date
    Jun 2007
    Posts
    36
    Quote Originally Posted by Rashakil Fol View Post
    it's not the case that 3 < 4 = T. It's the case that 3 < 4 == 1. The comparison operators return 1 or 0.

    So x = 3 < 4 < 2 simplifies to x = (3 < 4) < 2 which simplifies to 1 < 2 which simplifies to 1.
    okay, that makes perfect sense. Gotcha !

    thanks!

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,302
    Make sure you set your compiler to the highest warning levels so that it can point out silly mistakes such as comparing mismatching types. Then fix those errors without resorting to casting.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  2. C++ FTP class won't work
    By lord mazdak in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2005, 06:57 AM
  3. C++ Operator Overloading help
    By Bartosz in forum C++ Programming
    Replies: 2
    Last Post: 08-17-2005, 12:55 PM
  4. Reducing Code size from ridiculous length
    By DanFraser in forum C# Programming
    Replies: 10
    Last Post: 01-18-2005, 04:50 PM
  5. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 10:06 PM

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