Short Answer Review Questions:
6. What is an infinite loop? Write the code for an infinite loop.
An infinite loop is a sequence of instructions in a computer program which loops endlessly.
Example of an infinite loop:
Set k = 1
While k < = 5
Display k
End While
7. A FOR loop looks like what other loop in a flowchart?
A For loop looks like a count-controlled loop.
8. Why is it critical that accumulator variables are properly initialized?
An accumulator is used to keep a running total of numbers. In a loop, a value is usually added to the current value of the accumulator. If it is not properly initialized, it will not contain the correct total.
9. What is the advantage of using a sentinel?
The advantage of using a sentinel is that when you are processing a long list of values with a loop a sentinel marks the end of a list of items. There is no limit to how many times a loop can execute.
10. Why must the value chosen for use as a sentinel be carefully selected?
The value of a sentinel needs to be carefully selected because it can’t be mistaken as a regular value in the list.
Algorithm Workbench:
3. // Declare a counter variable.
Declare Integer
// Constant for the maximum value
Constant Integer MAX_VALUE = 100
//Display the multiples of 10 from 0 through 100
For counter = 0 to MAX_VALUE
Display counter
End while
4. //Declare Integer number
Declare an accumulator variable
Declare Integer total = 0
Declare Integer counter
For counter = 1 to 10
Display “Enter a number.”
Input number
Set total = total + number
End While
Display “The total is”, total
9. Declare Integer count
For count < 50
Display “The count is “, count
Set count = 1 To 50
End While
Programming Exercises
Average Rainfall
Declare integer years, months, inches
For years= 1 to 10 For months= 1 to 12 For inches= per month
Declare integer total= 0
Declare integer counter
Display “This program calculates the”
Display “average