Questions tagged [operators]

Operators are symbols that occur in nearly all programming and coding languages, for performing calculations and comparisons on data.

There are three main types of operators:

  • Arithmetic operators such as add (+), subtract (-), times (*), divide (/) and other numerical calculations.

  • Comparison operators, which give the result of a comparison between numbers or strings, for example greater than (>), less than (<), equal to (==).

  • Logical operators include things like AND (&&) and OR (||).

There are other operators, such as assignment and concatenation.

Use this tag for any questions relating to operators, in any language, including things such as syntax, behaviour and use.

7102 questions
9401
votes
26 answers

What is the "-->" operator in C/C++?

After reading Hidden Features and Dark Corners of C++/STL on comp.lang.c++.moderated, I was completely surprised that the following snippet compiled and worked in both Visual Studio 2008 and G++ 4.4. Here's the code: #include int main() { …
GManNickG
  • 459,504
  • 50
  • 465
  • 534
6670
votes
27 answers

Does Python have a ternary conditional operator?

If Python does not have a ternary conditional operator, is it possible to simulate one using other language constructs?
Devoted
  • 90,341
  • 41
  • 85
  • 110
5660
votes
49 answers

Which equals operator (== vs ===) should be used in JavaScript comparisons?

I'm using JSLint to go through JavaScript, and it's returning many suggestions to replace == (two equals signs) with === (three equals signs) when doing things like comparing idSele_UNVEHtype.value.length == 0 inside of an if statement. Is there a…
bcasp
  • 57,427
  • 4
  • 17
  • 14
4722
votes
20 answers

Reference — What does this symbol mean in PHP?

What is this? This is a collection of questions that come up every now and then about syntax in PHP. This is also a Community Wiki, so everyone is invited to participate in maintaining this list. Why is this? It used to be hard to find questions…
Gordon
  • 296,205
  • 68
  • 508
  • 534
3728
votes
11 answers

Why don't Java's +=, -=, *=, /= compound assignment operators require casting?

Until today, I thought that for example: i += j; Was just a shortcut for: i = i + j; But if we try this: int i = 5; long j = 8; Then i = i + j; will not compile but i += j; will compile fine. Does it mean that in fact i += j; is a shortcut for…
3497
votes
37 answers

What is the !! (not not) operator in JavaScript?

I saw some code that seems to use an operator I don't recognize, in the form of two exclamation points, like so: !!. Can someone please tell me what this operator does? The context in which I saw this was, this.vertical = vertical !== undefined ?…
Hexagon Theory
  • 37,347
  • 5
  • 24
  • 30
2255
votes
7 answers

What are the basic rules and idioms for operator overloading?

Note: The answers were given in a specific order, but since many users sort answers according to votes, rather than the time they were given, here's an index of the answers in the order in which they make the most sense: The General Syntax of…
sbi
  • 204,536
  • 44
  • 236
  • 426
2095
votes
4 answers

What does the ??!??! operator do in C?

I saw a line of C that looked like this: !ErrorHasOccured() ??!??! HandleError(); It compiled correctly and seems to run ok. It seems like it's checking if an error has occurred, and if it has, it handles it. But I'm not really sure what it's…
Peter Olson
  • 121,487
  • 47
  • 188
  • 235
1480
votes
15 answers

Is there a "null coalescing" operator in JavaScript?

Is there a null coalescing operator in Javascript? For example, in C#, I can do this: String someString = null; var whatIWant = someString ?? "Cookies!"; The best approximation I can figure out for Javascript is using the conditional operator: var…
1438
votes
11 answers

What are bitwise shift (bit-shift) operators and how do they work?

I've been attempting to learn C in my spare time, and other languages (C#, Java, etc.) have the same concept (and often the same operators) ... What I'm wondering is, at a core level, what does bit-shifting (<<, >>, >>>) do, what problems can it…
900
votes
10 answers

Behaviour of increment and decrement operators in Python

I notice that a pre-increment/decrement operator can be applied on a variable (like ++count). It compiles, but it does not actually change the value of the variable! What is the behavior of the pre-increment/decrement operators (++/--) in Python?…
Ashwin Nanjappa
  • 68,458
  • 72
  • 198
  • 283
885
votes
3 answers

JavaScript plus sign in front of function expression

I’ve been looking for information about immediately invoked functions, and somewhere I stumbled on this notation: +function(){console.log("Something.")}() Can someone explain to me what the + sign in front of the function means/does?
jOpacic
  • 9,083
  • 11
  • 31
  • 57
598
votes
11 answers

What is the use of the @ symbol in PHP?

I have seen uses of @ in front of certain functions, like the following: $fileHandle = @fopen($fileName, $writeAttributes); What is the use of this symbol?
sv_in
  • 13,469
  • 9
  • 32
  • 55
567
votes
7 answers

What's the difference between equal?, eql?, ===, and ==?

I am trying to understand the difference between these four methods. I know by default that == calls the method equal? which returns true when both operands refer to exactly the same object. === by default also calls == which calls equal?... okay,…
denniss
  • 15,528
  • 26
  • 84
  • 135
554
votes
11 answers

"is" operator behaves unexpectedly with integers

Why does the following behave unexpectedly in Python? >>> a = 256 >>> b = 256 >>> a is b True # This is an expected result >>> a = 257 >>> b = 257 >>> a is b False # What happened here? Why is this False? >>> 257 is 257 True …
Greg Hewgill
  • 828,234
  • 170
  • 1,097
  • 1,237
1
2 3
99 100