... int total{0};
 8
 9      // total even integers from 2 through 20
10      for (unsigned int number{2}; number <= 20; number += 2) {
11         total += number;                                      
12      }                                                        
13
14      cout << "Sum is " << total << endl;
15   }

Sum is 110

The initialization and increment expressions can be comma-separated lists that enable you to use multiple initialization expressions or multiple increment expressions. For example, although this is discouraged, you could merge the for statement’s body (line 11) into the increment portion of the for header by using a comma as in


total += number, number += 2

The comma between the expressions total += number and number += 2 is the comma operator, which guarantees that a list of expressions evaluates from left to right. The comma operator ...

Get C++ How to Program, 10/e now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.