Below are a set of code examples demonstrating the use of the 'for' statement. The first one represents the basic 'for' loop as described in Chapter 3, Section VIII.
// A program to add up 10 expenses using a for loop
// file: ch3xmpl6.cpp
#include <iostream.h>
const int NUM_TIMES_TO_LOOP = 10;
void main()
{
// Purpose: A program to add up 10 expenses using a for loop
// Receives: NONE
// Returns: NONE
char expenseCode;
double expense;
double totalExpenses = 0;
for (int numExpenses = 0; numExpenses < NUM_TIMES_TO_LOOP;
numExpenses++)
{ cout << "Please enter an expense\n";
cin >> expense;
totalExpenses += expense;
}
cout << "The " << NUM_TIMES_TO_LOOP << " expenses totaled $"
<< totalExpenses << endl;
}