Programming Logic and Design Chap. 5 - Looping
You have an outer for loop that will execute five times, with an inner for loop that will execute three times each time it is entered. How many times in total will the inner for loop execute?
15 (three times in each of the five iterations of the outer loop)
If the loop control variable is not altered within the loop, what will happen?
An infinite loop will occur
Four Ways to Change Loop Control Variable
Assign a new value to the loop control value Retrieve a new value from an input device Increment, or increase the loop control variable Decrement, or reduce the loop control variable
What are the four most common loop mistakes?
Failing to initialize the loop control variable Neglecting to alter the loop control variable Using the wrong type of comparison when testing the loop control variable Including statements inside the loop body that belong outside the loop
What are the three actions that should occur using a loop control variable as discussed on page 179?
Loop control variable is initialized before entering the loop Loop control variable value is tested, and if the results are true, the loop body is entered Loop control variable is altered within the body of the loop so that the tested condition that starts the loop eventually is false
What are three facts about nested loops as indicated on page 189?
Note that the number of times a loop executes may depend on a constant or on a variable that changes
What is the difference between count and accumulate?
Similar to a counter Counter increments by 1 Accumulator increments by some value
If the loop control variable is not initialized, what may happen?
The loop may not execute at all or may execute an incorrect number of times.
What are the two things that control a loop's repetition as discussed on pages 179-181?
Use a counter to create a definite, counter-controlled loop Use a sentinel value to create an indefinite loop
What are three advantages of using loops in a program as discussed on page 177?
Using fewer instructions results in less time required for design and coding Fewer errors Shorter compile time
What are the three steps that must occur in every loop as discussed on page 183?
You must provide a starting value for the variable that will control the loop You must test the loop control variable to determine whether the loop body executes Within the loop, you must provide a way to alter the loop control variable
Inner loop
a loop contained within another when loops are nested
What are the three actions that are performed automatically within the for statement?
a loop control variable is initialized, tested and altered
Counted loop or counter-controlled loop
a loop whose repetitions are managed by a counter
Step value
a number you use to increase a loop control variable on each pass through a loop
Defensive programming
a technique you employ when trying to prepare for all possible errors before they occur
Loop control variable
a variable that determines whether a loop will continue.
Accumulator
a variable that you use to gather or accumulate values
A variable that is used to hold a running total is called a(n) ___________ variable.
accumulator
Counter
any numeric variable you use to count the number of times an event has occurred
When can a structured loop be exited?
at the comparison test
In a for loop, is the condition tested before or after the loop body is executed?
before
for statement, or for loop
can be used to code definite loops. The for statement contains a loop control variable that it automatically initializes, evaluates, and increments
Outer loop
contains another loop when loops are nested
Another term for "counting down" is ____.
decrementing
What are the three steps found in every loop?
initialize the loop control variable, compare the loop control variable, alter the loop control variable
Summary reports
list only totals, without individual detail records
Indefinite loop
loop for which you cannot predetermine the number of executions.
Definite loop
loop in which the number of repetitions is a predetermined value
Nested loops
occur when a loop structure exists within another loop structure
do-while loop
posttest loop in which the body executes before the loop-controlling condition is tested
A report that contains only totals and other overall statistical information is called a(n) _____ report.
summary
GIGO ("garbage in, garbage out")
term that means if your input is incorrect, your output is worthless
Posttest loop
tests the loop control variable after each iteration; the loop statements execute at least once
Pretest loop
tests the loop control variable before each iteration; the loop statements may never execute
Decrement
to decrease a variable by a constant value, frequently 1
Increment
to increase a variable by adding a constant value to it, frequently 1
Validate data
to make sure data items are meaningful and useful. For example, you ensure that values are the correct data type or that they fall within an acceptable range
What are the possible actions that can be taken when data is not valid?
· Display and error message and stop the program · Use a default value that is valid · Reprompt the user for valid input
What are the three differences you must take with accumulators?
· Initialize the accumulator to 0 · Accumulators are altered: once for every data set processed · At the end of processing, accumulators are output
Facts about Nested Loops
· Nested loops never overlap. An inner loop is always completely contained within an outer loop. · An inner loop goes through all of its iterations each time its outer loop goes through just one iteration · Total number of iterations executed by a nested loop is the number of inner loop iterations times the number of outer loop iterations