Check if int is between two numbers

后端 未结 10 1056
夕颜
夕颜 2020-11-28 13:09

Why can\'t do you this if you try to find out whether an int is between to numbers:

if(10 < x < 20)

Instead of it, you\'ll have to do

10条回答
  •  天命终不由人
    2020-11-28 13:58

    You are human, and therefore you understand what the term "10 < x < 20" suppose to mean. The computer doesn't have this intuition, so it reads it as: "(10 < x) < 20".

    For example, if x = 15, it will calculate:

    (10 < x) => TRUE

    "TRUE < 20" => ???

    In C programming, it will be worse, since there are no True\False values. If x = 5, the calculation will be:

    10 < x => 0 (the value of False)

    0 < 20 => non-0 number (True)

    and therefore "10 < 5 < 20" will return True! :S

提交回复
热议问题