Comparison operators, as their name implies, allow to compare two values and usually return Boolean value (true or false).
Questions tagged [comparison-operators]
755 questions
2080
votes
26 answers
How to check if the string is empty in Python?
Does Python have something like an empty string variable where you can do:
if myString == string.empty:
Regardless, what's the most elegant way to check for empty string values? I find hard coding "" every time for checking an empty string not as…

Joan Venge
- 315,713
- 212
- 479
- 689
1707
votes
2 answers
Difference between == and === in JavaScript
What is the difference between == and === in JavaScript? I have also seen != and !== operators. Are there more such operators?

Shiva
- 18,235
- 4
- 20
- 9
490
votes
8 answers
Why does "not(True) in [False, True]" return False?
If I do this:
>>> False in [False, True]
True
That returns True. Simply because False is in the list.
But if I do:
>>> not(True) in [False, True]
False
That returns False. Whereas not(True) is equal to False:
>>> not(True)
False
Why?

Texom512
- 4,785
- 3
- 16
- 16
434
votes
10 answers
Is there a "not equal" operator in Python?
How would you say "does not equal"?
if hi == hi:
print "hi"
elif hi (does not equal) bye:
print "no hi"
Is there something similar to == that means "not equal"?

Aj Entity
- 4,741
- 4
- 17
- 13
301
votes
6 answers
What is the Ruby <=> (spaceship) operator?
What is the Ruby <=> (spaceship) operator? Is the operator implemented by any other languages?

Justin Ethier
- 131,333
- 52
- 229
- 284
174
votes
12 answers
169
votes
1 answer
Find substring in the string in TWIG
I want to find substring of the string or check if there is no such substring using Twig. On the words, I need analogue of 'strstr' or 'strpos' in php.
I googled and searched this issue in stackoverflow but nothing found. Does someone know how to…

user1440167
- 1,913
- 2
- 12
- 12
163
votes
5 answers
Difference between "!==" and "==!"
Yesterday I stumbled over this when I modified PHP code written by someone else. I was baffled that a simple comparison (if ($var ==! " ")) didn't work as expected. After some testing I realized that whoever wrote that code used ==! instead of !==…

Gerald Schneider
- 17,416
- 9
- 60
- 78
155
votes
4 answers
Is the operation "false < true" well defined?
Does the C++ specification define:
the existence of the 'less than' operator for boolean parameters, and if so,
the result of the 4 parameter permutations?
In other words, are the results from the following operations defined by the…

duncan
- 2,323
- 3
- 17
- 21
143
votes
9 answers
Why does the expression 0 < 0 == 0 return False in Python?
Looking into Queue.py in Python 2.6, I found this construct that I found a bit strange:
def full(self):
"""Return True if the queue is full, False otherwise
(not reliable!)."""
self.mutex.acquire()
n = 0 < self.maxsize ==…

Marcelo Santos
- 1,851
- 1
- 12
- 14
131
votes
8 answers
No == operator found while comparing structs in C++
Comparing two instances of the following struct, I receive an error:
struct MyStruct1 {
MyStruct1(const MyStruct2 &_my_struct_2, const int _an_int = -1) :
my_struct_2(_my_struct_2),
an_int(_an_int)
{}
std::string…

Jonathan Livni
- 101,334
- 104
- 266
- 359
69
votes
11 answers
Detecting negative numbers
I was wondering if there is any way to detect if a number is negative in PHP?
I have the following code:
$profitloss = $result->date_sold_price - $result->date_bought_price;
I need to find out if $profitloss is negative and if it is, I need to echo…

BigJobbies
- 4,293
- 12
- 42
- 51
63
votes
2 answers
Why is "!=" used with iterators instead of "<"?
I'm used to writing loops like this:
for (std::size_t index = 0; index < foo.size(); index++)
{
// Do stuff with foo[index].
}
But when I see iterator loops in others' code, they look like this:
for (Foo::Iterator iterator = foo.begin();…

Maxpm
- 24,113
- 33
- 111
- 170
62
votes
3 answers
How is the three-way comparison operator different from subtraction?
There's a new comparison operator <=> in C++20. However I think in most cases a simple subtraction works well:
int my_strcmp(const char *a, const char *b) {
while (*a == *b && *a != 0 && *b != 0) {
a++, b++;
}
// Version 1
…

iBug
- 35,554
- 7
- 89
- 134
61
votes
2 answers
Why is operator!= removed in C++20 for many standard library types?
According to cppreference, std::type_info::operator!= gets removed with C++20, however, std::type_info::operator== apparently remains.
What's the reasoning behind? I might agree on comparing for inequality being meaningless, but then comparing for…

Aconcagua
- 24,880
- 4
- 34
- 59