What Does the Comma Operator Do

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.

What does the comma operator do in JavaScript?

The comma operator evaluates both of
its operands (from left to right) and
returns the value of the second
operand.

Source: https://developer.mozilla.org/en/JavaScript/Reference/Operators/Special_Operators/Comma_Operator

For example, the expression 1,2,3,4,5 evaluates to 5. Obviously the comma operator is useful only for operations with side-effects.

console.log(1,2,3,4,5);console.log((1,2,3,4,5));

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

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 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 mean?

This expression

A, B = 0.0, 2;

is an expression with the comma operator (here are two comma operators). It can be presented like

( A ), ( B = 0.0 ), ( 2 );

As result the variable B will get the value 0.0. The variable A will be unchanged.

From the C Standard (6.5.17 Comma operator)

2 The left operand of a comma operator is evaluated as a void
expression; there is a sequence point between its evaluation and that
of the right operand. Then the right operand is evaluated; the result
has its type and value

So the value of the above expression is 2 and the type is int. The value of the expression is not used. So the only its side effect is assigning the value 0.0 to the variable B.

What does a comma mean inside an 'if' statement?

The programmer has used the comma operator to provide two unrelated expressions in a single statement. Because it's a single statement, both expressions are "inside" the if condition.

It's a poor hack, which would be better done with actual {} braces surrounding two statements.

Your example is not equivalent; it should be:

if (g[i][j] == 0) 
{
dfs(g, i, j);
++regions;
}

Please explain the comma operator in this program

The , operator evaluates a series of expressions and returns the value of the last.

c=a,b is the same as (c=a),b. That is why c is 10

c=(a,b) will assign the result of a,b, which is 20, to c.

As Mike points out in the comments, assignment (=) has higher precedence than comma

Uses of C comma operator

C language (as well as C++) is historically a mix of two completely different programming styles, which one can refer to as "statement programming" and "expression programming". As you know, every procedural programming language normally supports such fundamental constructs as sequencing and branching (see Structured Programming). These fundamental constructs are present in C/C++ languages in two forms: one for statement programming, another for expression programming.

For example, when you write your program in terms of statements, you might use a sequence of statements separated by ;. When you want to do some branching, you use if statements. You can also use cycles and other kinds of control transfer statements.

In expression programming the same constructs are available to you as well. This is actually where , operator comes into play. Operator , is nothing else than a separator of sequential expressions in C, i.e. operator , in expression programming serves the same role as ; does in statement programming. Branching in expression programming is done through ?: operator and, alternatively, through short-circuit evaluation properties of && and || operators. (Expression programming has no cycles though. And to replace them with recursion you'd have to apply statement programming.)

For example, the following code

a = rand();
++a;
b = rand();
c = a + b / 2;
if (a < c - 5)
d = a;
else
d = b;

which is an example of traditional statement programming, can be re-written in terms of expression programming as

a = rand(), ++a, b = rand(), c = a + b / 2, a < c - 5 ? d = a : d = b;

or as

a = rand(), ++a, b = rand(), c = a + b / 2, d = a < c - 5 ? a : b;

or

d = (a = rand(), ++a, b = rand(), c = a + b / 2, a < c - 5 ? a : b);

or

a = rand(), ++a, b = rand(), c = a + b / 2, (a < c - 5 && (d = a, 1)) || (d = b);

Needless to say, in practice statement programming usually produces much more readable C/C++ code, so we normally use expression programming in very well measured and restricted amounts. But in many cases it comes handy. And the line between what is acceptable and what is not is to a large degree a matter of personal preference and the ability to recognize and read established idioms.

As an additional note: the very design of the language is obviously tailored towards statements. Statements can freely invoke expressions, but expressions can't invoke statements (aside from calling pre-defined functions). This situation is changed in a rather interesting way in GCC compiler, which supports so called "statement expressions" as an extension (symmetrical to "expression statements" in standard C). "Statement expressions" allow user to directly insert statement-based code into expressions, just like they can insert expression-based code into statements in standard C.

As another additional note: in C++ language functor-based programming plays an important role, which can be seen as another form of "expression programming". According to the current trends in C++ design, it might be considered preferable over traditional statement programming in many situations.

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

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) {


Related Topics



Leave a reply



Submit