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
Q

How would you calculate an average by combining accumulation and counting

A
  • *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
Q

What is a common error with performing division

A

forgetting to check whether the denominator is 0 before performing a division

27
Q

What occurs if we declare total and count as integers and then calculate the average

A

If we declare total and count as integers then average will be calculated using integer division which truncates the remainder

28
Q

How would you get a floating-point average

A

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
Q

How would you try to get a maximum or minimum value

A

Use the running maximum or minimum to find it

30
Q

How would you go about getting the maximum

A
  • 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
Q

What generally occurs if a user enters a different data type then our request

A

The scanner next methods generate an InputMismatchException

or the program terminates

32
Q

How can we solve the problem of improper input from our users

A

Before the scanner reads the line, it can check whether the next input token matches our expected input.

33
Q

What Scanner class method would you use to check if the next input token can be interpreted as the data type specified

A

The hasNext method of the Scanner class will return true if the next input token can be interpreted as the data type specified

34
Q

What needs to be done if the hasNext method returns the result of false

A

We need to notify the user that the value typed is not valid and reprompt for new input

35
Q

In a sentinel controlled loop, when would you want to stop the loop

A

In a sentinel controlled loop we want to stop the loop when the input value is the sentinel.

36
Q

What is a loop termination condition

A

For a sentinel-controlled loop, we want to stop the loop when the input value is the sentinel.

37
Q

When would you want the loop to keep on executing in a sentinel controlled loop

A

We would want to keep on executing the loop if the input value is not the sentinel

38
Q

What is a loop continuation condition

A

When you keep on executing the loop if the input value is not the sentinel

39
Q

What is the first step to constructing a loop condition

A
  1. Define the loop termination condition, that is, define the condition that will make the loop stop executing
40
Q

What is the second step to constructing a loop condition

A
  1. 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
Q

What is the third step to constructing a loop condition

A
  1. Simplify the loop continuation condition by applying DeMorgan’s Laws where possible.
42
Q

What are the two DeMorgan’s laws

A

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
Q

What are the three questions to ask yourself when testing for while loops

A
  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
Q

What is particular about the do/while loop in relation to the while loop

A

Unlike the while loop, the condition for the do/while

loop is evaluated at the end of the loop.

45
Q

How many times does the do/while loop execute

A

A do/while loop executes at least once because the condition is evaluated at the end of the loop.

46
Q

What are some uses of a do/while loop

A

Validate user input

Ask if the user wants to repeat an operation, such as play a game again

47
Q

What is the do/while flow of control

A

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
Q

What occurs if the user input in a do/while flow is not valid

A

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
Q

Why isn’t it advised to use an if statement to validate user input

A

Do not use an if statement to validate input because it will catch invalid values entered the first time only.

50
Q

What is the benefit to using a do/while loop versus an if statement to validate user input

A

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
Q

When is the for loop ideally used

A

Ideal when you know the number of iterations to perform before the loop begins

52
Q

What are semicolons used for in the loop header

A

Semicolons separate terms in the loop header

53
Q

When are curly brackets required with a loop

A

Curly brackets are required only if more than one statement is in the loop body

54
Q

What is the for loop flow of control

A

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
Q

When is a loop control variable convenient

A

A loop control variable is convenient for counting

iterations of for loops.

56
Q

What is an important way to test for loops

A

Ensure that the starting and ending values of the loop variable are set correctly.

57
Q

What are nested loops

A

Nested loops are where the body of one loop contains another loop

58
Q

What is a flag variable used for

A

A flag variable is used to find something.

59
Q

How would you use a flag variable

A

– 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.