I l@ve RuBoard Previous Section Next Section

29.4 The Comma Operator

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 Previous Section Next Section