How Does the Comma Operator Work

What does the comma operator , do?

The expression:

(expression1,  expression2)

First expression1 is evaluated, then expression2 is evaluated, and the value of expression2 is returned for the whole expression.

How does the Comma Operator work

It would be equal to b.

The comma operator has a lower precedence than assignment.

What does the comma operator (expression, expression) means in Javascript when doing condition evaluation? [duplicate]

My question, what does this expression mean?

From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comma_Operator

The comma operator evaluates each of its operands (from left to right) and returns the value of the last operand (right most one).

Example:

if (y = f(x), y > x) {
// do something ...
}

This executes first the y = f(x) which sets up a value for y (that depends on x). Then checks if y > x. If True then do something, otherwise do nothing. It is the same as:

y = f(x);
if (y > x) {
// do something ...
}

In your examples:

(true, true & false)
// process `true` (does nothing)
// returns the result of `true & false` => false

(true, true & true)
// process `true` (does nothing)
// returns the result of `true & true` => true

(false, true & false)
// process `false` (does nothing)
// returns the result of `true & false` => false

(false, true & true)
// process `false` (does nothing)
// returns the result of `true & true` => true

Further examples:

(true, true, true, false) // returns false
(false, false, false, true) // returns true

So, in your application code:

if (console.debug(`[NotificationOpenedHandler] Received additionalData:`, e), e.type && e.id) {

Is the same as:

console.debug(`[NotificationOpenedHandler] Received additionalData:`, data);
if(data.type && data.id) {

How does the comma operator work in js? [duplicate]

var c = (a,b);

The above uses the comma operator. It evaluates as the value of its right-hand side (i.e. b).


var c = a,b;

This does not use the comma operator.

The comma character here forms part of the var expression which takes a comma-separated list of variables to create in the current scope, each of which can have an optional assignment.

It is equivalent to:

var c = a;
var b;

What is the proper use of the comma operator?

In your example it serves no reason at all. It is on occasion useful when written as

if(cond)
perror("an error occured"), exit(1) ;

-- then you don't need curly braces. But it's an invitation to disaster.

The comma operator is to put two or more expressions in a position where the reference only allows one. In your case, there is no need to use it; in other cases, such as in a while loop, it may be useful:

while (a = b, c < d)
...

where the actual "evaluation" of the while loop is governed solely on the last expression.

Comma operator in c [duplicate]

Can any one explain how output is 2?

In the statement

a = (1, 2), 3;   

, used is a comma operator.
Due to higher operator precedence of = operator than that of , operator, the expression operand (1, 2) will bind to = as

(a = (1, 2)), 3;  

In case of comma operator, the left operand of a comma operator is evaluated to a void expression, then the right operand is evaluated and the result has the value and type of the right operand.

There are two comma operators here. For the first comma operator in the expression (1, 2), 1 will be evaluated to void expression and then 2 will be evaluated and will be assigned to a.

Now side effect to a has been taken place and therefore the right operand of second comma operator 3 will be evaluated and the value of the expression (a = (1, 2)), 3 will be 3.

How is the comma operator being used here? [duplicate]

This code is equivalent to this:

int a = 2 ; 
int b = a + 1 ;

First expression to the left of comma gets evaluated, then the one to its right. The result of the right most expression is stored in the variable to the left of = sign.

Look up the comma operator for more details.

http://en.wikipedia.org/wiki/Comma_operator

How does comma operator work during assignment?

Yes, that's exactly it: the compiler takes the last value. That's the comma operator, and it evaluates its operands left-to-right and returns the rightmost one. It also resolves left-to-right. Why anyone would write code like that, I have no idea :)

So int b = (1, 2, 3) is equivalent to int b = 3. It's not a primitive list of any kind, and the comma operator , is mostly used to evaluate multiple commands in the context of one expression, like a += 5, b += 4, c += 3, d += 2, e += 1, f for example.

Some confusion with how commas work in C/C++ [duplicate]

The comma operator is easy — so easy it is hard. It has the lowest priority of all operators; its priority is even lower than the assignment operators. Note that arguments to functions are not separated by the comma operator.

The comma operator evaluates its left-hand operand, generates a sequence point and discards the result, and then evaluates the right-hand operand.

In context:

x++, ++x, x += x;

is equivalent to:

x++;
++x;
x += x;

except that the overall value is the result of x += x;.

Given that x starts at 16, it is incremented to 17, then 18, then doubled to 36. The overall value is therefore 36.

Note that because of the sequence points, it does not run foul of the rules about not incrementing the same variable more than once between sequence points.

The only reason for using a comma operator really is that there are contexts where you can't use separate statements but you can use comma operators. For example:

for (i = 0, j = n; i < j; ++i, --j)

You can't use semicolons in place of those commas.


In the question, there are two samples:

int y = (x++, ++x, x+=x);

int y = x++, ++x, x+=x;

The first is legitimate (though unnecessarily contorted), and initializes y to 36 (and sets x to 36).

The second is not legitimate and won't compile; the commas are not comma operators and should be separating distinct declarators, but the ++x and x += x are not declarators. However, if it was changed to:

y = x++, ++x, x+=x;

then it would be legitimate. The first term is:

y = x++

which assigns 16 to y and increments x to 17. The second term increments x to 18; the third term changes x to 36.

Post-increment operator behaviour w.r.t comma operator?

The comma operator is a sequence point: as it says in the C++17 standard for example,

Every value computation and side effect associated with the left expression is sequenced
before every value computation and side effect associated with the right expression.

Thus, the effect of the ++ operator is guaranteed to occur before 999+j is evaluated.



Related Topics



Leave a reply



Submit