Professional Documents
Culture Documents
The Comma Operator: Example
The Comma Operator: Example
The Comma Operator: Example
Any of the three expressions in a for statement can be omitted, however, you must type
at least two semicolons. The shortest loop header is therefore:
Example: for(;;)
This statement causes an infinite loop, since the controlling expression is assumed to be
true if expression2 is missing. In the following
The comma operator separates the assignments for the variables i and limit and is
then used to calculate and output the value of x in a single statement.
The comma operator has the lowest precedence of all operators — even lower than
the assignment operators. This means you can leave out the parentheses in the above
example.
Like any other C++ expression, an expression containing the comma operator has a
value and belongs to a certain type. The type and value are defined by the last expression
in a statement separated by commas.
Example: x = (a = 3, b = 5, a * b);
In this example the statements in brackets are executed before the value of the product
of a * b is assigned to x.