Examples - while loops
#include <iostream.h>
int main ( )
{
int count = 1;
while ( count <= 10 )
{
cout << count << " "
;
count++;
}
return 0;
}
#include <iostream.h>
#include <iomanip.h>
int main ( )
{
int num;
cout << "NUMBER SQUARE
CUBE\n"
<< "________
________ _____\n";
num = 1;
while ( num <= 11 )
{
cout << setw(3) << num
<< " "
<<
setw(3) << num * num << " "
<< setw(4) << num * num * num <<endl;
num++;
}
return 0:
}
#include <iostream.h>
int main ( )
{
const int HIGHGRADE = 100;
int num = 0;
float grade, total, average;
grade = total = average = 0.0;
cout << "Please enter the quiz grades" <<
endl;
cout << "When all grades have been entered, enter
any number greater than 100" << endl;
while ( grade <= HIGHGRADE )
{
total = total + grade;
// Or total += grade;
cout << "\nEnter a grade: ";
cin >> grade;
if ( grade <= HIGHGRADE )
num++;
}
cout << "\nThe total of the grades is " <<
total << " and " << num
<< " students
took the quiz\n";
if ( num == 0 )
cout << "No grades were entered,
unable to compute an average grade" << endl;
else
cout << "The quiz average is
" << total/num << endl;
return 0;
}