Questions tagged [operators]

Operators are symbols that occur in nearly all programming and coding languages, used for performing calculations, comparisons, and assignments of data. Use this tag for any questions relating to operators, in any language, including things such as syntax, behaviour and use.

There are four 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 (||).

  • Binary operators perform bitwise operations on the binary representation of variables such as AND (&), OR (|), NOT (~), XOR(^), LSHIFT (<<), and RSHIFT (>>).

There are other operators, such as assignment and concatenation.

7764 questions
10074
votes
28 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. I would assume this is also valid C since it…
GManNickG
  • 494,350
  • 52
  • 494
  • 543
7779
votes
32 answers

Does Python have a ternary conditional operator?

Is there a ternary conditional operator in Python?
Devoted
  • 177,705
  • 43
  • 90
  • 110
5649
votes
48 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,397
  • 4
  • 19
  • 15
5054
votes
25 answers

Reference Guide: What does this symbol mean in PHP? (PHP Syntax)

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
  • 312,688
  • 75
  • 539
  • 559
4093
votes
42 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
  • 43,627
  • 5
  • 26
  • 30
3837
votes
12 answers

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

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…
2526
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
  • 139,199
  • 49
  • 202
  • 242
2440
votes
8 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
  • 219,715
  • 46
  • 258
  • 445
1805
votes
5 answers

What is ':-!!' in C?

I bumped into this strange macro code in /usr/include/linux/kernel.h: /* Force a compilation error if condition is true, but also produce a result (of value 0 and type size_t), so the expression can be used e.g. in a structure initializer (or…
chmurli
  • 15,080
  • 3
  • 16
  • 12
1557
votes
19 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…
1539
votes
10 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)... At a core level, what does bit-shifting (<<, >>, >>>) do, what problems can it help solve, and what…
1043
votes
11 answers

Behaviour of increment and decrement operators in Python

How do I use pre-increment/decrement operators (++, --), just like in C++? Why does ++count run, but not change the value of the variable?
Ashwin Nanjappa
  • 76,204
  • 83
  • 211
  • 292
912
votes
4 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,453
  • 12
  • 36
  • 58
633
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,929
  • 9
  • 34
  • 55
616
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
  • 951,095
  • 183
  • 1,149
  • 1,285
1
2 3
99 100