HPlogo HP C/HP-UX Reference Manual: Version A.05.55.02 > Chapter 5 Expressions and Operators

Comma Operator (,)

» 

Technical documentation

Complete book in PDF

 » Table of Contents

 » Index

Syntax

exp1, exp2

Arguments

exp1

Any expression.

exp2

Any expression.

Description

Use the comma operator to separate two expressions that are to be evaluated one right after the other. The comma operator is popular within for loops, as demonstrated by the following example:

for (i = 10, j = 4; i * j < n; i++, j++);

In the preceding example, the comma operator allows you to initialize both i and j at the beginning of the loop. The comma operator also allows you to increment i and j together at the end of each loop iteration.

All expressions return values. When you use a comma operator, the expression returns the value of the rightmost expression. For example, the following statement sets variable j to 2:

j = (x = 1, y = 2);

Assignments such as these, however, are considered poor programming style. You should confine use of the comma operator to for loops.

© Hewlett-Packard Development Company, L.P.