Looping

In the Branching exercise, you had to run Program Grade five times in order to try the program with five different input values.


A copy of Program Sum in file progL8.cpp is shown below.

 
// Program Shell1 prompts for and reads a one-digit number.
// Values between 0 and the digit (inclusive) are summed.
#include <iostream>
using namespace std;
int main ()
{
  int  counter; // loop-control variable
  int  sum;            // running sum
  int  digit;
 
  cout  << "Enter a one-digit number; press return."
        << endl;
  cin  >> digit;
  counter =            // Initialize counter                     
  sum  =               // Initialize sum                    
 
  while // While expression                                                  
  {                                                       
 
    // Body of the loop                              
 
  }
  cout  << "Sum of digits between 0 and "
        << digit  << " is "  << sum  << endl;
  return 0;
}

 

Read he program carefully and do the following tasks.

Compile the program.

Run the program using 9 as the input value. What is printed?

Is the loop a count-controlled loop or an event-controlled loop?

In order to change the loop to the other type of loop, what would have to be changed?