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
What is an easy way to calculate an average
Combine accumulation and counting
How would you calculate an average by combining accumulation and counting
- *Start by initializing a total variable and a count variable to 0
- *Each time you read an item, add its value to the total variable and increment the count variable.
- When you have no more items to read, calculate the average by dividing the total by the count
What is a common error with performing division
forgetting to check whether the denominator is 0 before performing a division
What occurs if we declare total and count as integers and then calculate the average
If we declare total and count as integers then average will be calculated using integer division which truncates the remainder
How would you get a floating-point average
To get a floating point average, we need to type cast one of the variables (either total or count) to a double or a float to force the division to be performed as a floating point
How would you try to get a maximum or minimum value
Use the running maximum or minimum to find it
How would you go about getting the maximum
- Read the first item and save its value as the current maximum
- Each time we read a new value, we compare it to the current maximum (if it is larger then it becomes new maximum)
- When you have no more items to read, current maximum is the max for all values
What generally occurs if a user enters a different data type then our request
The scanner next methods generate an InputMismatchException
or the program terminates
How can we solve the problem of improper input from our users
Before the scanner reads the line, it can check whether the next input token matches our expected input.
What Scanner class method would you use to check if the next input token can be interpreted as the data type specified
The hasNext method of the Scanner class will return true if the next input token can be interpreted as the data type specified
What needs to be done if the hasNext method returns the result of false
We need to notify the user that the value typed is not valid and reprompt for new input
In a sentinel controlled loop, when would you want to stop the loop
In a sentinel controlled loop we want to stop the loop when the input value is the sentinel.
What is a loop termination condition
For a sentinel-controlled loop, we want to stop the loop when the input value is the sentinel.
When would you want the loop to keep on executing in a sentinel controlled loop
We would want to keep on executing the loop if the input value is not the sentinel
What is a loop continuation condition
When you keep on executing the loop if the input value is not the sentinel
What is the first step to constructing a loop condition
- Define the loop termination condition, that is, define the condition that will make the loop stop executing
What is the second step to constructing a loop condition
- Create the loop continuation condition—the condition that will keep the loop executing—by applying the Logical NOT operator ( ! ) to the loop termination condition.
What is the third step to constructing a loop condition
- Simplify the loop continuation condition by applying DeMorgan’s Laws where possible.
What are the two DeMorgan’s laws
NOT( A AND B ) is equivalent to ( NOT A ) OR ( NOT B )
NOT( A OR B ) is equivalent to ( NOT A ) AND ( NOT B )
What are the three questions to ask yourself when testing for while loops
- Does the program produce correct results with a set
of known input values? - Does the program produce correct results if the sentinel value is the first and only input?
- Does the program deal appropriately with invalid input?
What is particular about the do/while loop in relation to the while loop
Unlike the while loop, the condition for the do/while
loop is evaluated at the end of the loop.
How many times does the do/while loop execute
A do/while loop executes at least once because the condition is evaluated at the end of the loop.
What are some uses of a do/while loop
Validate user input
Ask if the user wants to repeat an operation, such as play a game again
What is the do/while flow of control
The do/while loop body is executed, then the condition is checked. If the condition is true, the loop body is repeated. If the condition is false, the loop body is not repeated
What occurs if the user input in a do/while flow is not valid
If the user input is not valid, we want to reprompt until the user enters a valid value. Thus, we form the condition so that it is true if the user enters invalid data.
Why isn’t it advised to use an if statement to validate user input
Do not use an if statement to validate input because it will catch invalid values entered the first time only.
What is the benefit to using a do/while loop versus an if statement to validate user input
A do/while loop will continue to prompt the user until the user enters a valid value whereas an if statement will only catch invalid values entered the first time only.
When is the for loop ideally used
Ideal when you know the number of iterations to perform before the loop begins
What are semicolons used for in the loop header
Semicolons separate terms in the loop header
When are curly brackets required with a loop
Curly brackets are required only if more than one statement is in the loop body
What is the for loop flow of control
The initialization statement is executed (once only).
• The loop condition is evaluated.
• If the condition is true, the loop
body is executed.
• The loop update statement is
then executed, and the loop condition is reevaluated.
When is a loop control variable convenient
A loop control variable is convenient for counting
iterations of for loops.
What is an important way to test for loops
Ensure that the starting and ending values of the loop variable are set correctly.
What are nested loops
Nested loops are where the body of one loop contains another loop
What is a flag variable used for
A flag variable is used to find something.
How would you use a flag variable
– We set the flag to false before starting the for loop
that checks for factors.
– Inside the for loop, we set the flag to true when we find a factor.
– After the for loop terminates, we check the value of the flag. If it is still false, we did not find any factors, and the number is prime.