Questions tagged [operators]

For questions about operators in programming languages. Operators are built-in tokens that perform specific operations on their operands distinct from functions.

Filter by
Sorted by
Tagged with
-1 votes
0 answers
33 views

Should addition assignment `a += b` be equivalent to addition then assignment `a = a + b` [closed]

I am looking for the pros and cons of a += b being an exact synonymous, or not, of a = a + b. The question also holds regarding <...
Stef's user avatar
  • 244
8 votes
6 answers
3k views

Why do relational comparison operators never short-circuit?

I just thought about the possibility for the less-than and-greater than operators to short-circuit. That is, they can skip evaluating their second operand if the value of the second operand logically ...
user16217248's user avatar
  • 7,285
42 votes
6 answers
12k views

Why do programming languages use the asterisk * for multiplication?

Having had very little math(s) education I'm trying to bring myself up to speed for university, which currently involves teaching myself the times tables, where X ...
Hashim Aziz's user avatar
3 votes
4 answers
227 views

Common postfix representations of pointer/reference operations

In some C-based languages (including C/C++ themselves and Rust), taking pointers/references and dereferencing is done with prefix operators & and ...
abel1502's user avatar
  • 2,459
12 votes
5 answers
857 views

Prior art on pipelines of function calls

Say I have a loop that looks like this: for i in range(1, 10) { print(i) } Now I want to take the same sequence in reverse, and filter out even numbers. I might ...
Rob N's user avatar
  • 1,043
17 votes
3 answers
3k views

Why does MATLAB have left division/solve?

In MATLAB (and Octave), the \ (or mldivide) and \. operators are provided with the exact ...
Adám's user avatar
  • 3,028
17 votes
9 answers
3k views

Why add an increment/decrement operator when compound assignments exist?

Many languages have compound assignment statements, for example +=. However, for the common task of adding or subtracting 1 from a variable some languages (mostly ...
Seggan's user avatar
  • 2,594
9 votes
4 answers
379 views

What are the advantages of keyword based syntax versus punctuation based syntax?

Some languages such as Lua and Assembly Basic use keywords for syntax. This includes spelling out words such as do and end in ...
user16217248's user avatar
  • 7,285
2 votes
3 answers
182 views

Pros and cons of generalized `nil`-coalescing operators

Many languages extend their "indexing" operator . to a nil-coalescing variant ?. to deal with indexing chains such as <...
Luatic's user avatar
  • 288
5 votes
6 answers
309 views

Ways to have operators for both normal and floor division

I want to have an operator for true division (like / in python) giving a float as the result and floor division (like // in ...
MarcellPerger's user avatar
9 votes
6 answers
2k views

What syntax could be used to implement both an exponentiation operator and XOR?

C does not have an exponentiation operator, so that freestanding implementations that lack the math libraries could lack exponentiation as a whole, and still be permissible by the C standard. As a ...
user16217248's user avatar
  • 7,285
16 votes
5 answers
1k views

What are the downsides of supporting chained comparison operators?

Some languages, such as Python, support chaining comparison operators, such that x < y < z is equivalent to ...
Rydwolf Programs's user avatar
5 votes
9 answers
584 views

What are pros/cons of ternary conditional operators?

There are some languages that support ternary operator. There are Python, C, JavaScript, PHP. In languages like C and JavaScript, the syntax is like this: ...
nchistov's user avatar
  • 1,521
2 votes
3 answers
95 views

Pre-increment and post-increment and augmented assignment but no post-augmented assignment?

In C-style languages we have: int x = 10; x++; // x is now 11 but the expression returns 10 ...
user16217248's user avatar
  • 7,285
11 votes
2 answers
381 views

What are some caveats to be careful about when implementing the `?:` (ternary conditional) operator?

Thus far, I see two things that can go wrong when implementing the ?: (ternary conditional) operator: PHP famously incorrectly implemented the ...
FlatAssembler's user avatar
6 votes
2 answers
272 views

If size could be determined at compile time then why could size not be determined at preprocess time?

In C the sizeof() operator cannot be resolved in preprocessor conditions. If other operators such as + or ...
user16217248's user avatar
  • 7,285
8 votes
4 answers
340 views

What are the potential problems with assignment operators returning the new-value operand and how can they be avoided or mitigated?

Inline assignment is when the assignment operator can return the value of the new-value (right-hand) operand. For example: ...
user16217248's user avatar
  • 7,285
8 votes
3 answers
303 views

Unambiguous syntax for a ternary comparison operator

How could an operator to test if a number is within a lower and upper bound? SQL seems to have this as in 10 <= x <= 20. However, if this were to be ...
user16217248's user avatar
  • 7,285
14 votes
7 answers
595 views

Ternary operators other than ?: [closed]

In many programming languages, there is a ternary operator such as X ? Y : Z or Y if X else Z. This can be used as an ...
FireTheLost's user avatar
  • 1,553
12 votes
6 answers
368 views

What are the disadvantages of this far-fetched idea: All integers can be treated as boolean arrays?

I do not know of any languages that support this idea but I just thought that since numbers are just bits, why should we not be able to access those bits directly? In most languages such as C or C++, ...
user16217248's user avatar
  • 7,285
8 votes
1 answer
159 views

How to distinguish custom operators from each other?

How might one go about building a parser that can distinguish custom operators in prefix, infix or postfix positions. For example: ...
kaiserthe13th's user avatar
5 votes
2 answers
276 views

What are the pros and cons of each type of operator notation (prefix/postfix/infix)?

In Prefix notation, the operator comes before the operands (e.g., +a b). In Postfix notation, the operator comes after the operands (e.g., ...
The Thonnu's user avatar
  • 1,474
1 vote
3 answers
483 views

Why is the Elvis operator not more common?

From Wikipedia: In certain computer programming languages, the Elvis operator, often written ?:, is a binary operator that returns its first operand if that ...
justANewbie's user avatar
13 votes
5 answers
445 views

Distinguishing modulo (Euclidean division) from remainder

The % operator in most programming language provide a remainder from integer division. When both operands are positive all works as expected. But when the number is ...
user16217248's user avatar
  • 7,285
7 votes
5 answers
420 views

Why are expressions in languages so prone to integer overflow?

I've always wondered. Why do major languages let their addition and multiplication operators be so prone to integer overflow? My programming language would have separate operators to handle overflow. ...
Dannyu NDos's user avatar
10 votes
6 answers
510 views

How can ambiguity with unary and binary operators be resolved?

Most languages use +/- for addition and subtraction, in addition to using +/...
Rydwolf Programs's user avatar
6 votes
6 answers
422 views

Advantages/disavantages of including the comma operator?

In many C style languages the , binary operator takes 2 expressions and simply returns the second one: ...
mousetail's user avatar
  • 6,746
16 votes
15 answers
1k views

What are the syntax options for implementing a ternary "if" operator?

The ternary "if" operator is a syntactic construct for returning one of two values based upon a boolean condition. In most C-family languages its syntax resembles ...
Ginger's user avatar
  • 2,451
14 votes
11 answers
793 views

What alternatives are there for C++ operator overloading syntax?

I noticed C++ has operator overloading syntax with just the operator names: T& operator +(T& lhs, T& rhs) { return /* ... */; } However, there are ...
user16217248's user avatar
  • 7,285