29.4 The Comma Operator

I l @ ve RuBoard

The comma operator (,) can be used to group statements. For example:

 if (total < 0) {      std::cout << "You owe nothing\n";      total = 0;  } 

can be written as:

 if (total < 0)       std::cout << "You owe nothing\n", total = 0; 

In most cases, { } should be used instead of a comma. About the only place the comma operator is useful is in a for statement. The following for loop increments two counters, two and three , by 2 and 3:

 for (two = 0, three = 0;       two < 10;       two += 2, three += 3)           std::cout << two << ' ' << three << '\n'; 
I l @ ve RuBoard


Practical C++ Programming
Practical C Programming, 3rd Edition
ISBN: 1565923065
EAN: 2147483647
Year: 2003
Pages: 364

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net