Chapter 6 PDF Flashcards

1
Q

What is the flow of control that programmers use to complete jobs with a repetitive pattern called

A

Looping or iteration

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the while loop designed for

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How is the end of data items indicated generally

A

By a special input value called a sentinel value or by reaching the end of file

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is is called when you are doing a loop and there is a signal that we are at the end

A

Receiving the signal is an event and this is called event controlled looping

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is special about variables defined within the while loop

A

Any variable defined within the while loop has block scope and cannot be referenced after the while loop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Describe the while loop flow of control

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Describe the operation of the while loop

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does the loop body generally do

A

The loop body usually updates the loop condition - it performs some operation that eventually will cause the loop condition to evaluate to false.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the loop update used for

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is an iteration

A

One execution of the loop body

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is a loop update

A

One ar more statements that could cause the loop condition to evaluate to false (and thus end the looping)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is a loop termination condition

A

The event that causes the loop condition to evaluate to false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is an endless or infinite loop

A

If the loop condition never values to false, the loop body is executed continuously without end

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How can we tell if we have an infinite loop

A
  • 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How can we stop an infinite loop

A

Abort the program

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is a common error associated with a while loop

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is a common error with a sentinel controlled while loop

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What are the standard patterns and techniques for performing common programming operations (looping)

A
Accumulation
Counting Items
Finding an average
Finding maximum or minimum values
Animation
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What is accumulation

A

Calculating a total of input values

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What is the approach in accumulation (running count)

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What is a common error with accumulation

A

Forgetting to initialize the total variable to 0 before beginning the loop will produce incorrect results

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What is an easy way to count items (ie: how many people bought a regular ticket, a childs ticket etc)

A

Using the running count.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

How would you use the running count

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

What is an easy way to calculate an average

A

Combine accumulation and counting

25
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
26
What is a common error with performing division
forgetting to check whether the denominator is 0 before performing a division
27
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
28
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
29
How would you try to get a maximum or minimum value
Use the running maximum or minimum to find it
30
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
31
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
32
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.
33
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
34
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
35
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.
36
What is a loop termination condition
For a sentinel-controlled loop, we want to stop the loop when the input value is the sentinel.
37
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
38
What is a loop continuation condition
When you keep on executing the loop if the input value is not the sentinel
39
What is the first step to constructing a loop condition
1. Define the loop termination condition, that is, define the condition that will make the loop stop executing
40
What is the second step to constructing a loop condition
2. Create the loop continuation condition—the condition that will keep the loop executing—by applying the Logical NOT operator ( ! ) to the loop termination condition.
41
What is the third step to constructing a loop condition
3. Simplify the loop continuation condition by applying DeMorgan's Laws where possible.
42
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 )
43
What are the three questions to ask yourself when testing for while loops
1. Does the program produce correct results with a set of known input values? 2. Does the program produce correct results if the sentinel value is the first and only input? 3. Does the program deal appropriately with invalid input?
44
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.
45
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.
46
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
47
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
48
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.
49
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.
50
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.
51
When is the for loop ideally used
Ideal when you know the number of iterations to perform before the loop begins
52
What are semicolons used for in the loop header
Semicolons separate terms in the loop header
53
When are curly brackets required with a loop
Curly brackets are required only if more than one statement is in the loop body
54
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.
55
When is a loop control variable convenient
A loop control variable is convenient for counting | iterations of for loops.
56
What is an important way to test for loops
Ensure that the starting and ending values of the loop variable are set correctly.
57
What are nested loops
Nested loops are where the body of one loop contains another loop
58
What is a flag variable used for
A flag variable is used to find something.
59
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.