Summary: Outlines several uses of the sequence operator within the C++ programming language.
Note: You are viewing an old version of this document. The latest version is available here.
The sequence (or comma) operator is used to separate items. It has several uses, four of which are listed then demonstrated:
int pig, dog, cat, rat;
This is often seen in textbooks, but this method of declaring variables is not preferred. It is difficult to quickly read the identifier names.
int pig;
int dog;
int cat;
int rat;
This vertical method of declaring variables or constants is preferred.
double area_trapezoid(double base, double height, double top);
The data types and identifier names (known as parameters) are separated from each other.
for( x = 1, y = 5; x < 15; x++, y--)
In the syntax of a for loop you have three parts each separated by a semi-colon. The first is the initialization area which could have more than one initialization. The last is the update area which could have more than one update.
int ages[] = {2,4,6,29,32};
The variable ages is an array of integers. Initial values are assigned using block markers with the values separated from each other.