Chapter 6 PDF Flashcards
What is the flow of control that programmers use to complete jobs with a repetitive pattern called
Looping or iteration
What is the while loop designed for
The while loop is designed for repeating a set of operations on data items when we don’t know how many data items there will be
How is the end of data items indicated generally
By a special input value called a sentinel value or by reaching the end of file
What is is called when you are doing a loop and there is a signal that we are at the end
Receiving the signal is an event and this is called event controlled looping
What is special about variables defined within the while loop
Any variable defined within the while loop has block scope and cannot be referenced after the while loop
Describe the while loop flow of control
The condition is evaluated. If it is true then we execute the loop. Then the condition is reevaluated.
As long as the condition is true, we execute the loop body. When the condition is false, we skip to the instruction following the loop.
Describe the operation of the while loop
If the condition evaluates to true, then the loop body is executed and then the condition is reevaluated. As long as the condition evaluates to true, we continue to repeat the loop body
What does the loop body generally do
The loop body usually updates the loop condition - it performs some operation that eventually will cause the loop condition to evaluate to false.
What is the loop update used for
The loop update will be an attempt to read the next input value in order to detect the sentinel value or the end of the file
What is an iteration
One execution of the loop body
What is a loop update
One ar more statements that could cause the loop condition to evaluate to false (and thus end the looping)
What is a loop termination condition
The event that causes the loop condition to evaluate to false
What is an endless or infinite loop
If the loop condition never values to false, the loop body is executed continuously without end
How can we tell if we have an infinite loop
- If the loop body has no output, the computer appears to hang
- If the loop body produces output, the output is repeatedly written without end
How can we stop an infinite loop
Abort the program
What is a common error associated with a while loop
Avoid putting a semicolon after the condition of a while loop. Adding a semicolon after the condition creates an empty loop body and could result in an endless loop
What is a common error with a sentinel controlled while loop
- If you omit the update read it may result in an endless loop
- Omitting the priming read causes the sentinel value to be processed, leading to incorrect results
What are the standard patterns and techniques for performing common programming operations (looping)
Accumulation Counting Items Finding an average Finding maximum or minimum values Animation
What is accumulation
Calculating a total of input values
What is the approach in accumulation (running count)
We start by initializing a total variable is 0
Each time we read a value, we add the value to the total
When we have no more values to read the total is complete.
What is a common error with accumulation
Forgetting to initialize the total variable to 0 before beginning the loop will produce incorrect results
What is an easy way to count items (ie: how many people bought a regular ticket, a childs ticket etc)
Using the running count.
How would you use the running count
Start by initializing a count variable to 0
Each time you read a value, check whether that value meets the criteria for something we want to count. If so, increase count variable by 1
ETC