A comma operator (,) mainly evaluates its operands from left to right sequentially and returns the value of the rightmost operand. A comma operator is used as a separator for multiple expressions at a place that requires a single expression. When a comma operator is placed in an expression, it executes each expression and returns the rightmost expression.
Expression1, Expression2, Expression3, ....so on
In the above syntax, multiple expressions are separated using a comma operator. During execution, each expression will be executed from left to right and the rightmost expression will be returned.
Example: Below is an example of the Comma operator.
one two three three
Welcome to Geeksforgeeks Geeksforgeeks
In the output, first of all, the function x() is executed then y(), and lastly z(). Finally, the comma operator returns the rightmost expression.
Example: The most useful application of the comma operator is in loops. In loops, it is used to update multiple variables in the same expression.
0 5 1 4 2 3 3 2 4 1 5 0
- Google Chrome
- Internet Explorer
- Apple Safari