for ( each student, s, in this class )
assignGradeTo( s );
for ( each day, d, of the quarter )
studyHardOnDay( d );
for (each station, s, on the radio tuner )
{
tuneRadioTo( s );
if ( youLikeTheSong( listen() )
break; /// terminates this for loop
}
for ( each integer, i, in the range 0 to 9 )
cout << i << endl;
// print out numbers 0 through 9
for ( int i = 0; i < 10; i++ )
cout << i << endl;
// read 10 integers from the input and print the sum
int main()
{
int valueRead = 0;
int sumTotal = 0;
for ( int i = 0; i < 10; i++ )
{
cin >> valueRead;
sumTotal += valueRead;
}
cout << "The total is: " << sumTotal << endl;
}
while ( isTooSour(coolade) )
addATeaspoonOfSugar(coolade);
while ( isTooCold(bathtub) )
addGallonsOfHotWater(bathtub, 1);
while ( ! understandTheHomeworkAssignment(student) )
{
readTheHomeworkHandout( student );
askQuestions( TA, student );
}
while ( isStillAwake( student ) )
study(student);
do
turnIgnition( car );
while (! started( car ) );
do
pressANumber( phone );
while (! haveAConnection( phone ) );
do
{
readTheHomeworkHandout( student );
askSomeQuestions( TA, student );
} while ( ! understands( student, materialForWeek( w ) ) );
do
eat( person, pintOfIceCream );
while ( !isSick( person ) );
const int JAN = 1, DEC = 12;
int main()
{
for ( int y = 2000; y <= 2010; y++ )
for ( int m = JAN; m <= DEC; m++ )
{
for ( int d = 1; d <= DAYS_PER_MONTH; d++ )
cout << m << "/" << d "/" << y << ' ';
cout << endl;
}
}
for (int i = 0; i < 10; i++ ) cout << i; cout << i; /// i is no longer in scope
for (int i = 0; i < 10; i+1 ) /// i+1 is not advancing cout << i; ... int i; /// may forget to initialize while ( i < 10 ) cout << i; /// not advancing!
for (int i = 0; i <= 10; i++ ) /// wrong < operator cout << i; ... for (int i = 1; i < 10; i++ ) /// wrong initial value cout << i;
int a[3] = {0, 1, 2};
for ( int i = 0; i < 3; i++ ) a[i] += 5; // add 5 to each element of array a
a[2] = a[0]; // assign value at a[0] into memory at a[2]
for ( int i = 0; i < 3; i++ ) cout << a[i] << endl;
const int A_LENGTH = 3;
int main()
int a[A_LENGTH];
...
for ( int i = 0; i < A_LENGTH; i++ )
out << a[i] << endl;
#include <iostream.h>
const int SUN = 0;
const int MON = 1;
const int TUE = 2;
const int WED = 3;
const int THU = 4;
const int FRI = 5;
const int SAT = 6;
const int DAYS_PER_WEEK = 7;
void initialize( int hoursWorked[] )
{
for ( int i = 0; i < DAYS_PER_WEEK; i++ )
hoursWorked[i] = 0;
}
void print( ostream & out, int hoursWorked[] )
{
for ( int i = 0; i < DAYS_PER_WEEK; i++ )
out << "On day "
<< i
<< " worked "
<< hoursWorked[i]
<< " hours\n";
}
void recordHours(int hoursWorked[], int i, int hours)
{
hoursWorked[i] = hours;
}
int totalHours( int hoursWorked[] )
{
int totalHours = 0;
for ( int i = 0; i < DAYS_PER_WEEK; i++ )
totalHours += hoursWorked[i];
return totalHours;
}
void reportTotals( int hoursWorked[] )
{
print( cout, hoursWorked );
cout << "Worked "
<< totalHours( hoursWorked )
<< " total hours this week\n";
}
int main()
{
int hoursWorked[DAYS_PER_WEEK];
initialize(hoursWorked);
recordHours(hoursWorked, MON, 8);
recordHours(hoursWorked, TUE, 9);
recordHours(hoursWorked, WED, 6);
recordHours(hoursWorked, THU, 9);
recordHours(hoursWorked, FRI, 4);
reportTotals(hoursWorked);
return 0;
}