Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do most programming languages only have binary equality comparison operators?

In natural languages, we would say "some color is a primary color if the color is red, blue, or yellow."

In every programming language I've seen, that translates into something like:

isPrimaryColor = someColor == "Red" or someColor == "Blue" or someColor == "Yellow" 

Why isn't there a syntax that more closely matches the English sentence. After all, you wouldn't say "some color is a primary color if that color is red, or that color is blue, or that color is yellow."

I realize simply isPrimaryColor = someColor == ("Red" or "Blue" or "Yellow") because instead of Red Blue and Yellow they could be boolean statement in which case boolean logic applies, but what about something like:

isPrimaryColor = someColor ( == "Red" or == "Blue" or == "Yellow") 

As an added bonus that syntax would allow for more flexibility, say you wanted to see if a number is between 1 and 100 or 1000 and 2000, you could say:

someNumber ((>= 1 and <=100) or (>=1000 and <=2000)) 

Edit:

Very interesting answers, and point taken that I should learn more languages. After reading through the answers I agree that for strictly equality comparison something similar to set membership is a clear and concise way of expressing the same thing (for languages that have language support for concise inline lists or sets and testing membership)

One issues that came up is that if the value to compare is the result of an expensive calculation a temporary variable would need to be (well, should be) created. The other issue is that there may be different evaluations that need to be checked, such as "the result of some expensive calculation should be prime and between 200 and 300"

These scenarios are also covered by more functional languages (though depending on the language may not be more concise), or really any language that can take a function as a parameter. For instance the previous example could be

MeetsRequirements(GetCalculatedValue(), f(x):x > 200, f(x):x < 300, IsPrime) 
like image 534
Davy8 Avatar asked Jul 08 '10 15:07

Davy8


People also ask

Are all programming languages equal?

Yes, programming languages are similar but not that much. The basics of every programming language are pretty much the same, but the way you write and use those basics to solve problems differs a lot for every language. Python and JavaScript are mostly used for the same things and code looks very similar.

What is the meaning of === in programming?

The strict equality operator ( === ) checks whether its two operands are equal, returning a Boolean result. Unlike the equality operator, the strict equality operator always considers operands of different types to be different.

Which operator is used for comparison?

The equality operator (==) is used to compare two values or expressions. It is used to compare numbers, strings, Boolean values, variables, objects, arrays, or functions. The result is TRUE if the expressions are equal and FALSE otherwise.

Why C++ is better than any other languages?

Faster: C/C++ is faster than any other programming language in terms of speed. The C++ source code needs to become machine code. Whereas, python follows a different tactic as it is interpreted. The compilation of code is always faster than the interpretation.


1 Answers

I think that most people consider something like

isPrimaryColor = ["Red", "Blue", "Yellow"].contains(someColor) 

to be sufficiently clear that they don't need extra syntax for this.

like image 56
mqp Avatar answered Sep 24 '22 05:09

mqp