CSCI 240 - Loops Worksheet 1

dowhile & while

REVIEW

A do..while loop is bottom driven, so the condition statement is at the bottom of the loop.
A while loop is top driven, so the condition statement is at the top of the loop.
Both structures require assigning a value to the test variable before the condition.

dowhile structure			while structure
do					testvariable = value;
  {					while (condition)
  statements				  {
   :					  statements
   :					   :
  testvariable = new value;		   :
  }					  testvariable = new value;
while (condition);			  }  

Write the following loops as both do..while and while structures. Many of the loops are similar. Focus on how they are the same. Note what it was that you had to change.

1. Accept a series of numbers entered from the keyboard. Continue until 0 (zero) is entered. Add each number to a sum before getting the next number.

2. Accept a series of numbers entered from the keyboard until a negative number is entered. Add each number to a sum. Do not include the negative number in the sum.

3. Accept a series of strings from the keyboard. Continue until the string EXIT, Exit, or exit is entered. For each string, print the statement, "You have entered ", and then print the string.
a) Print this statement even when the end marker is entered.
b) Do not print this statement when the end marker is entered.

4. Write a loop to add up the numbers from 6 to 25.

5. Write a loop to find the average of 10 quiz scores entered by the user. You know there will be exactly 10.

6. Write a loop to find the average of quiz scores entered by the user. You don't know how many the user will enter, but a negative 1 will be entered when done.