Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Relational operator
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Operator chaining=== In mathematics, it is common practice to chain relational operators, such as in 3 < x < y < 20 (meaning 3 < x ''and'' x < y ''and'' y < 20). The syntax is clear since these relational operators in mathematics are transitive. However, many recent programming languages would see an expression like 3 < x < y as consisting of two left (or right-) associative operators, interpreting it as something like <code>(3 < x) < y</code>. If we say that x=4, we then get <code>(3 < 4) < y</code>, and evaluation will give <code>true < y</code> which generally does not make sense. However, it does compile in C/C++ and some other languages, yielding surprising result (as ''true'' would be represented by the number 1 here). It is possible to give the expression <code>x < y < z</code> its familiar mathematical meaning, and some programming languages such as Python and [[Raku (programming language)|Raku]] do that. Others, such as C# and Java, do not, partly because it would differ from the way most other infix operators work in C-like languages. The [[D programming language]] does not do that since it maintains some compatibility with C, and "Allowing C expressions but with subtly different semantics (albeit arguably in the right direction) would add more confusion than convenience".<ref>{{cite book |last=Alexandrescu |first=Andrei |title=The D Programming Language |year=2010 |publisher=Addison Wesley |page=58 |isbn=978-0-321-63536-5}}</ref> Some languages, like [[Common Lisp]], use multiple argument predicates for this. In Lisp <code>(<= 1 x 10)</code> is true when x is between 1 and 10.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)