Question: Using C++ (please don't use somthing advanced or somthing new, I will post below the explanation from the class. we should try to not use

Using C++

(please don't use somthing advanced or somthing new, I will post below the explanation from the class. we should try to not use somthing that we did not learn yet.)

(the Exercise)

While Loop Exercise:

 

Get a copy of the count.cpp file.

-----------------------------

cpp file.

/ Program Count prompts for the number of integers to sum. // It then reads the integers in a loop, summing them. // Finally, it prints the sum. #include  using namespace std; int main () { int sum = 0; // summing variable int dataValue; // input value // add an int declaration statement here for the loop counter // add an int declaration statement here for the loop total // add an assignment statement here to set the loop counter to 0 // add a cout statement here to prompt the user how many numbers // he/she wants to add i.e. the loop total // add a cin statement here to get the loop total // add a while statement here to test the loop counter // against the loop total e.g. while (loopctr < looptotal) // Note: the variable names depend on how you declared them. { cout << "Enter an integer value. Press return." << endl; cin >> dataValue; sum = sum + dataValue; // add a statement here to increment your counter variable } // end of while loop cout << endl << "The sum is " << sum << endl; return 0; } 

---------------------------------------

For visual studio users, you can click on the above link then copy and past it.

For hercules users, you have the following options:

Entering the cp command: cp /net/data/ftp/pub/class/110/ftp/cpp/loop/count.cpp count.cpp

or, using your mouse to copy and paste the program into pico text edit window.

The count.cpp program demonstrates how to use a while loop.

Complete the program by following the instructions in the comments.

Compile and run the program. Use the the following input values to test your program: loop total: 5 five numbers to be added 3, -2, 0, 9, 1.

Here is a sample output

hercules[1]% CC count.cpp -o count hercules[2]% count Please input the number of integers that you would like to add: 5 Enter an integer value: 3 Enter an integer value: -2 Enter an integer value: 0 Enter an integer value: 9 Enter an integer value: 1 The sum of the 5 integers is 11 

--------------------------------------

expaination from note class

A while loop

The syntax of the while loop is:

while ( Expression ) { Statement } 

A while statement allows a program to continue executing a statement as long as the value of the Boolean expression is true. When the Boolean expression is evaluated as false, execution of the program continues with the statement immediately following the while statement.

Count-controlled while loop

Now let's look at the following C++ code:

sum = 0; count = 1; while (count <= 10) { cout << "Enter a value: "; cin >> value; sum = sum + value; count++; } cout << "The sum of the 10 numbers is " << sum << endl; 

The variables sum and count are assigned the values 0 and 1 respectively. The Boolean expression (count <= 10) is evaluated. Because the value in count is less than or equal to 10, the expression is true and the compound statement (block) associated with the While statement is executed. A number is extracted from the standard input stream and added to the sum. The value in count is incremented by 1.

The While expression is evaluated again. Because the value stored in the count is still less than 10, the compound satatement associated with the While statement is executed again. This process countinues until count contains the value 11. At that time, the expression is no longer true, the body of the While loop is not executed again, and the execution continues with the statement immediately following the While statement. In this case, it continues with a cout that sends the labeled answer to the output stream.

Count-controlled while loop

A count-controlled loop is one that is executed a certain number of times. The above example shows a count-controlled loop. We know that the program sends the answer to the output stream after excuting the loop body 10 times. Now let's look at another loop control structure --- event-controlled loop.

Back to the top

Event-controlled loop

An event-controlled loop is one whose execution is controlled by the occurence of an event whithin the loop itself. Now let's look at an example of an event-controlled loop that reads and sums value from cin until a negative value is encountered.

sum = 0; cout << "Enter a value: "; cin >> value; while (value >= 0) { sum = sum + value; cout << "Enter a value: "; cin >> value; } cout << "The sum of the values prior to a negative value is " << sum << endl; 

For this same problem, we can write a program in another way:

sum = 0; cout << "Enter a value: "; cin >> value; // Set the boolean variable moreData to true if the first data item is // not negative; false otherwise. if (value >= 0) moreData = true; else moreData = false; while (moreData) { sum = sum + value; cout << "Enter a value: "; cin >> value; // Reset the value of moreData if (value >= 0) moreData = true; else moreData = false; } cout << "The sum of the values prior to a negative value is " << sum << endl; 

sum is set to zero and the first data item value is read outside of the loop. value is compared to zero and its result is stored in the Boolean variable moreData. If the first data item is negative, moreData has a value of false, and the body of the loop is not executed. If the first data item is non-negative, moreData has a value of true, and the body of the loop is entered and executed. In the later case, value is added to the sum and the next data item is read. This new data item is compared to zero again, and the variable moreData is reset. The expression is tested again. This process continues until a value of negative is read and moreData becomes false. When this happens, the body of the While loop is not executed again. The sum of the non-negative numbers is sent to the output stream.

The above while loop is also called a flag-controlled loop. The variable moreData is the flag. It has the equivalent effect as the following:

sum = 0; cout << "Enter a value: "; cin >> value; // Set moreData to true if the first data item is not negative; // false otherwise. moreData = value >= 0; while (moreData) { sum = sum + value; cout << "Enter a value: "; cin >> value; // Reset the value of moreData moreData = value >= 0; } cout << "The sum of the values prior to a negative value is " << sum << endl; 

Proper Loop Operation

For obvious reasons, the While statement is called a loop or looping statement. The statement that is being executed within the loop is called the body of the loop.

There are three basic steps that must be done for a loop to operate properly.

The variables in the expression (the counter or event) must be set (initialized) before the while statement is executed the first time.

The expression must test the counter or event correctly so that the body of the loop executes when it is supposed to and terminates at the proper time.

The counter or the status of the event must be updated within the loop. If the counter or the status of the event is not updated, the loop never stops executing. This situation is called an infinite loop.

Back to the top

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!