Programming Exam Flashcards
Do the nested loops need to be the same loop type?
No, the nested loops do not need to be the same loop type.
When can nested loops be useful?
Nested loops can be useful if we are performing multiple operations, each of which having its own count or sentinel value
What is important about the inner (nested) loop ?
The inner, nested loop executes completely (executes all its iterations) for each single iteration of the outerloop
When does a loop repeat a set of operations for each input item?
A loop repeats a set of operations for each input item while a condition is true
What is the while loop especially useful for?
The while loop is especially useful for event-controlled looping.
How does a while loop work?
A while loop executes a set of operations in the loop body as long as the loop condition is true. Each execution of the loop body is an iteration of the loop
What occurs if the loop condition evaluates to false the first time it is evaluated?
If the loop condition evaluates to false the first time it is evaluated, the body of the while loop is never executed
What occurs if the loop condition never evaluates to false
The result is an infinite loop
How does event-controlled looping work
In event-controlled looping, processing of items continues until the end of the input is signaled either by a sentinel value or by reaching the end of the file
What is a sentinel value
A sentinel value is a special input that signals the end of the items to be processed.
What is the normal flow of control with a sentinel value
A priming read is performed before the while loop, then the body of the loop processes the input and then performs an update read on the next data item
How can we test that we have reached the end of the file when reading from an input file
We can test whether we have reached the end of the file by calling a hasNext method of the Scanner class
Describe the general setup of an accumulation programming technique
A total variable is initialized to 0 before the loop starts, then in the loop body we add each input value to the total. When the loop completes, the current total is the total for all of the processed input values
Describe the general setup of a counting program technique
Initialize a count variable to 0 before starting the loop. In the loop body, we increment the count variable for each input value that meets our criteria. When the loop completes, the count variable contains the number of items that met our criteria
Describe the general setup of finding an average
We have to combine accumulation and counting. We add the input values to the total and increment the count. When the loop completes, we calculate the average by dividing the total by the count. Before computing the average though, we should verify that the count != 0
Describe the general setup of finding a maximum or minimum value
We assign the first input to a running maximum or minimum. In the loop body, we compare each input value to our running max/min. In the loop body, we compare each input value to our running max/min. If the input value is less than the running min, we assign it to the running min. If it is greater than the running max, we assign the input value to the running max. When the loop completes, the running value is the max or min of all of the input values
How do we avoid generating exceptions when the user types characters other than the data type expected
We use the hasNext methods of the Scanner class
How do we construct a loop condition
We must construct the inverse of the loop termination condition
What are important things to test when testing a program that contains a loop?
Test that the program produces correct results by inputting values and comparing the results with manual calculations (avoid logical errors), also test that the results are correct if the loop body never executes. The final thing to test is what occurs when an invalid input is entered.
In what order does the do/while loop check the condition/execute the loop
The do/while loop checks the loop condition after executing the loop body.
How many times (minimum) does a do/while loop execute
A do/while loop executes at least once because the condition is checked after the loop body is executed.
When is it useful to use a do/while loop
When you want to validate input
Why/When are for loops used
The for loop is useful for count-controlled loops, aka loops in which the number of iterations is known before the loop begins
Briefly explain how for loops are executed
When the for loop is encountered, the initialization statement is executed. Then, the loop condition is evaluated. If the condition == true then the loop body executes. the loop update statement is then executed and the loop condition is reevaluated. If the loop condition is still true, it continues so on until the condition evaluates to false