Chapter 4 Flashcards

(93 cards)

1
Q

What is a loop in programming?

A

A program construct that repeatedly executes a set of statements while a condition is true

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

What is the loop body?

A

The statements executed repeatedly within a loop

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

What is an iteration?

A

One complete execution of the loop body

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

What real-world example illustrates looping?

A

Driving around the block until a baby falls asleep

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

What determines whether a loop continues or exits?

A

The loop expression’s evaluation (true = continue, false = exit)

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

What is a sentinel value?

A

A special input value that signals the end of data processing

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

What type of loop uses a sentinel value?

A

Loops that process input until a termination condition is met

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

What causes an infinite loop?

A

Forgetting to update a loop control variable or using the wrong loop expression

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

How do you prevent infinite loops?

A

Ensure loop variables are properly updated within the loop body

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

What is the basic structure of a while loop?

A

while expression is true, execute loop body, then recheck expression

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

What common task uses a loop to process lists?

A

Computing sums, averages, counts, or finding maximum/minimum values

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

In the baby asleep example, what represents the loop decision point?

A

Checking if the baby is awake

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

What happens when a loop’s expression evaluates to false?

A

Execution proceeds to the statement after the loop

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

When does a loop’s expression get evaluated?

A

Before each iteration of the loop body

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

What is a common mistake when writing loops?

A

Using the opposite expression than intended or forgetting input updates

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

What is a loop that never stops called?

A

An infinite loop

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

What is the purpose of initializing variables before a loop?

A

To ensure the loop body operates on correct starting values

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

What pattern allows processing unknown input amounts?

A

Using a sentinel value that marks the end of input

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

What is the output of this code with inputs 2, 4, 1, -1?

A

sum = 7 (sum adds inputs until -1 is encountered)

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

Why do we often use “val > -1” in loop expressions?

A

To exclude sentinel values like -1 from calculations

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

What happens if you forget to update the input within a loop?

A

The loop may become infinite or behave incorrectly

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

What does this loop accomplish: counting negative numbers?

A

It counts how many negative values are in a list

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

How can you find the maximum in a list using a loop?

A

Compare each input to current max and update if larger

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

What is wrong with initializing max to 0 for unknown lists?

A

If all list values are negative, max will be incorrect

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Why is initializing max with the first input preferred?
It ensures comparisons start with actual list data
26
What is the loop expression for "iterate while x is less than 100"?
x < 100
27
How do you iterate until a value equals 15?
Use the expression: while value != 15
28
What is an example of looping N times?
A loop with initialization, expression, and update controlling exact repetitions
29
What are the three essential parts of an N-times loop?
Initialization, loop expression, and update statement
30
What is the common pattern for looping 10 times?
i = 0; while i < 10; i = i + 1
31
How can loops create flexible output sequences?
By adjusting the loop variable's initialization, expression, or update step
32
What does the following loop output: i = 5; while i <= 15; i = i + 3?
Outputs: 5 8 11 14
33
What is a nested loop?
A loop placed inside the body of another loop
34
What is the difference between inner and outer loops?
The inner loop completes all iterations for each outer loop iteration
35
What task is well-suited for nested loops?
Creating tables, grids, or processing 2D data
36
What does this nested loop output: for row = 1 to 2, for col = 1 to 3?
Outputs: 2 3 4 3 4 5
37
What is the total number of inner loop executions if outer runs 4 times, inner runs 6?
24 total inner loop executions
38
In a nested loop, when does the inner loop variable reset?
At the start of each outer loop iteration
39
How can you output a multiplication table using nested loops?
Outer loop for rows, inner loop for columns, output row * column
40
How can nested loops create patterns like triangles?
Outer loop controls rows, inner loop controls items per row
41
What causes an infinite loop in this code: while x == 0?
If x is never updated within the loop body
42
What is a do-while loop?
A loop that executes the body first, then checks the condition
43
When is a do-while loop preferred?
When the loop must execute at least once (e.g., menu systems)
44
What is the main difference between while and do-while loops?
do-while guarantees at least one execution; while may skip entirely
45
Where is the condition evaluated in a do-while loop?
After the loop body executes
46
What happens if the condition is false from the start in a do-while loop?
The loop body still executes once
47
What is a for loop's structure?
Initialization; expression; update — all at the top of the loop
48
When is a for loop preferred?
When the number of iterations is known beforehand
49
How can you convert a for loop to a while loop?
Separate initialization before, use expression in while, update inside body
50
What is the order of execution in a for loop?
Initialization → condition check → body → update → repeat
51
What does this for loop output: for i = 0; i < 3; i++?
Outputs: 0 1 2
52
What happens if you modify the loop control variable inside a for loop?
It may cause incorrect iterations or infinite loops
53
What does this loop output: for num = 10; num >= 2; num -= 2?
Outputs: 10 8 6 4 2
54
What is the purpose of descriptive loop variable names?
Improves readability and code clarity
55
What happens in this while loop: i = 0; while i < 5; i++?
Outputs: 0 1 2 3 4
56
What expression creates an infinite loop: while x != 0, but x never changes?
The loop never exits because x stays constant
57
What task is best for a while loop?
Processing input until a termination condition is met
58
What task is best for a for loop?
Executing a known number of iterations
59
What happens if you omit updating the loop variable?
The loop may become infinite or logically incorrect
60
What causes off-by-one errors in loops?
Incorrect initialization or improper expression boundaries
61
What is output by this loop: i = 0; while i < 4; sum += 3; i++?
Outputs: sum = 5, 8, 11, 14 (if sum starts at 5)
62
Why is initializing a loop variable before the loop important?
Ensures correct starting values for loop execution
63
What does this code output: value = 100; while value >= 10; value /= 2?
Outputs: 100 50 25 12
64
How can you output multiples of 7 from 14 to 70?
Initialize at 14, loop while ≤ 70, increment by 7 each iteration
65
What task requires nested loops for full processing?
Creating a grid, pattern, or 2D table
66
What does the bar chart nested loop accomplish?
Prints lines of asterisks representing numeric values
67
How many times does the inner loop run if outer runs 3 times, inner runs 5?
15 total inner loop executions
68
What loop type guarantees at least one iteration?
do-while loop
69
What is the correct expression to iterate while x is not equal to 10?
x != 10
70
How do you loop exactly 25 times?
Initialize i = 0; loop while i < 25; increment i by 1
71
How can loops process lists of unknown length?
Use a sentinel value to mark the end of input
72
What is the role of a sentinel value?
To signal the end of an input list
73
What happens in Euclid's GCD algorithm when numA % numB = 0?
The loop terminates; numB contains the GCD
74
How can you process student grades until -1 is entered?
Loop while grade != -1, process each grade inside the loop
75
Why is Euclid's algorithm efficient for finding GCD?
It reduces the numbers quickly through division
76
What is the loop expression to output even numbers up to 50?
i = 2; while i ≤ 50; i += 2
77
How do you create a multiplication table using loops?
Nested loops: outer for rows, inner for columns, output product
78
What prevents infinite loops during input processing?
Always get new input within the loop body
79
What expression continues a loop while userNum is not -1?
userNum != -1
80
Why might a for loop be better than a while loop for array processing?
The number of iterations is pre-determined by array size
81
How can you loop down from 40 to -10 in steps of -5?
Initialize at 40, loop while ≥ -10, decrement by 5 each iteration
82
What is the importance of loop variable updates?
Prevents infinite loops and controls loop progression
83
When would you use a while loop over a for loop?
When the number of iterations depends on dynamic conditions
84
How can you validate user input with a loop?
Loop until valid input is received, checking conditions inside the loop
85
What does this loop do: while x < 1000; x *= 2?
Finds the first power of 2 greater than or equal to 1000
86
Why use nested loops for 2D arrays?
To process rows and columns systematically
87
What happens to complexity with additional nested loops?
It increases, often exponentially with each level
88
How do you create Pascal's triangle with loops?
Nested loops: outer controls rows, inner calculates combinations
89
What loop type is ideal for menu systems?
do-while loop, to guarantee menu displays at least once
90
How can you find both max and min in a list?
Loop through all values, update max and min as needed
91
What causes off-by-one errors in for loops?
Incorrect expression boundaries like i <= N instead of i < N
92
Why is it important to use descriptive loop variable names?
Improves code clarity and understanding of loop purpose
93
What loop pattern generates every 3rd element of a list?
Initialize index = 0; loop while < size; increment by 3 each time