Questions tagged [comparison-operators]

Comparison operators, as their name implies, allow to compare two values and usually return Boolean value (true or false).

658 questions
1664
votes
25 answers

How to check if the string is empty?

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
  • 269,545
  • 201
  • 440
  • 653
1663
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?
482
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,361
  • 2
  • 13
  • 15
279
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
  • 122,367
  • 49
  • 219
  • 273
171
votes
12 answers

Why is === faster than == in PHP?

Why is === faster than == in PHP?
coderex
  • 24,487
  • 43
  • 111
  • 167
162
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
  • 16,520
  • 9
  • 55
  • 76
154
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,233
  • 2
  • 14
  • 21
146
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,683
  • 2
  • 11
  • 12
106
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
  • 84,911
  • 94
  • 244
  • 345
60
votes
1 answer

Why does C++11 contain an odd clause about comparing void pointers?

While checking the references for another question, I noticed an odd clause in C++11, at [expr.rel] ¶3: Pointers to void (after pointer conversions) can be compared, with a result defined as follows: If both pointers represent the same address or…
Matteo Italia
  • 115,256
  • 16
  • 181
  • 279
58
votes
10 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,033
  • 10
  • 39
  • 51
58
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
  • 30,581
  • 7
  • 64
  • 105
56
votes
10 answers

What does "===" mean?

I've noticed someone using the PHP operator === which I can't make sense out of. I've tried it with a function, and it corresponds in crazy ways. What is the definition of this operator? I can't even find it in the declaration of PHP operators.
Stefan Konno
55
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
  • 20,568
  • 29
  • 91
  • 159
53
votes
0 answers

Removed operator!= in C++20 standard library

According to cppreference, the operator!= of many standard library types, including std::unordered_map::operator!= and std::unordered_set::operator!= is removed in C++20. What is the committee's rationale behind the decision? Won't this make the…
plasmacel
  • 7,355
  • 5
  • 42
  • 87
1
2 3
43 44