This is the comma operator. It "wraps" multiple expressions, evaluates them from left to right, and the value of the whole expression is determined by the last sub-expression. In your example, it evaluates to 3
.
A situation where the comma operator is especially handy is if you want to do multiple things in a for-loop "increment" expression, for example to increment two variables.
Example: Iterate an image along the diagonal, using x
and y
as separate variables. I use two separate variables for x
and y
because I might want to change one of them in the loop independently from the other (remember, it's just a stupid example). So I want to increment both x
and y
in the "increment" statement of the for-loop:
for(int x = 0, y = 0; x < width && y < height; ++x, ++y) {
// ... ^^^^^^^^
}
Note that the "initialization" expression of the for-loop does not use the comma operator; it just declares two variables.