// This program illustrates the use of a sentinel in a while loop. // The user is asked for monthly rainfall totals until a sentinel // value of -1 is entered. Then the total rainfall is displayed. #include using namespace std; int main() { // Fill in the code to define and initialize to 1 the variable month float total = 0, rain; cout << "Enter the total rainfall for month " << month << endl; cout << "Enter -1 when you are finished" << endl; // Fill in the code to read in the value for rain // Fill in the code to start a while loop that iterates // while rain does not equal -1 { // Fill in the code to update total by adding it to rain // Fill in the code to increment month by one cout << "Enter the total rainfall in inches for month " << month << endl; cout << "Enter -1 when you are finished" << endl; // Fill in the code to read in the value for rain } if (month == 1) cout << "No data has been entered" << endl; else cout << "The total rainfall for the " << month-1 << " months is "<< total << " inches." << endl; return 0;
Types of Loop
Loops are the elements of programming in which a part of code is repeated a particular number of times. Loop executes the series of statements many times till the conditional statement becomes false.
Loops
Any task which is repeated more than one time is called a loop. Basically, loops can be divided into three types as while, do-while and for loop. There are so many programming languages like C, C++, JAVA, PYTHON, and many more where looping statements can be used for repetitive execution.
While Loop
Loop is a feature in the programming language. It helps us to execute a set of instructions regularly. The block of code executes until some conditions provided within that Loop are true.
// This program illustrates the use of a sentinel in a while loop.
// The user is asked for monthly rainfall totals until a sentinel
// value of -1 is entered. Then the total rainfall is displayed.
#include using namespace std;
int main() {
// Fill in the code to define and initialize to 1 the variable month
float total = 0, rain;
cout << "Enter the total rainfall for month " << month << endl; cout << "Enter -1 when you are finished" << endl;
// Fill in the code to read in the value for rain
// Fill in the code to start a while loop that iterates
// while rain does not equal -1 {
// Fill in the code to update total by adding it to rain
// Fill in the code to increment month by one
cout << "Enter the total rainfall in inches for month " << month << endl; cout << "Enter -1 when you are finished" << endl;
// Fill in the code to read in the value for rain }
if (month == 1)
cout << "No data has been entered" << endl; else
cout << "The total rainfall for the " << month-1 << " months is "<< total << " inches." << endl; return 0;
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 3 images