Quiz4-RepetitionStructures Flashcards
The following pseudocode is for a loop that will perform three iterations: (T/F)
For start = 6 To 2 step -3
Do something
End For
False. iterations is 2x
meaning 6-3=3
3-3= 0
The following pseudocode is for a loop that will perform three iterations: (T/F)
For start = 5 To 10 Step 2
Do something
End For
True:
start at 5 skip 2.
UNTIL value is 10.
5+2=7
7+2=9
9+2=11
STOP ITERATIONS BC 11>10
The Do-While loop is a posttest loop (T/F)
True
The while loop gets its name from the way it works: it does a task WHILE a condition is false (T/F)
False
while a condition is TRUE.
The test condition in a While loop must always be an integer value(T/F)
False
A while loop repeats infinitely when there is no statement inside the loop body that will make the test condition false(T/F)
True
For loops automatically increment or decrement a counter variable(T/F)
True
You can only use positive integers as step values in a For statement(T/F)
False
In a For loop the programmer must know the exact number of iterations the loop must perform before writing the code(T/F)
False
While and For loops are considered pretest loops because they test the condition before processing the statement or statements in the loop body(T/F)
True
A condition-controlled loop can be used to iterate the body of the loop a specific number of times(T/F)
True
In the following pseudocode counter is the accumulator(T/F):
For counter = 1 to 6
Set numberWidgets=numberwidgets+counter
End For
False:
The variable numberofwigdets is used as an accumulator to keep track of a running total as the loop iterates. The accumulator is incremented by the value of counter on each iteration of the loop.
After the loop has completed, the final value of the accumulator would be the sum of the numbers from 1 to 6
Which of the following is the structure that causes a statement or set of statements to execute repeatedly?
repetition structure
Which type of loop uses a Boolean expression to control the number of times a loop repeats?
Condition-Controlled loop
Which type of loop repeats a statement or set of statements as long as the Boolean expression IS FALSE?
Do-Until
Which type of loop is specifically designed to initialize, test, and increment or decrement a counter variable?
For loops
The amount by which the counter variable is incremented or decremented in a For loop is known as the:
Step amount
A loop that accumulates a total as it reads each number from a series is often said to keep a(n):
running total
Statements that appear between the While and the End While statements are known as the:
Loop Body
A(n) ___________ represents a special value that marks the end of a list of values
Sentinel
The following is an example of a ____________ loop:
While x < 10
Set sum = sum + x
Set x = x + 1
End While
Count-Controlled
If an expression is False, the ________ operator will return True.
NOT
How many times would the following loop iterate?
For j = 1 To 5 step 2
Display j
End For
A) 0
B) 5
C) 3
C) 3 iterations:
j=1
count up from 1 to including 5(limit) bc j is allowed to be any value 1 thru and including 5. count up and skip 2
1+2=3
3+2=5
5+2=7
7>5 so the loop ends
How many times would the following loop iterate?
Set k = 1
While k <= 5
Display k
End While
infinite
bc k is not stepped it will always = 1
and 1 always <= 5
How many times would the following loop iterate?
Set k = 1
While k > 5
Display k
End While
0 iterations
k=1 and will always be less than 5 NOT greater. therefore the loop will not execute bc the condition is not met
How many times would the following loop iterate?
Set k = 1
Do
Display k
Set k = k +1
Until k > 2
2 iterations:
k=1
1+1=2
2+1=3
3>2 therefore loop ends
How many times would the following loop iterate?
Set k = 1
While k < 5
Display k
Set k = k + 1
End While
4 iterations:
k=1 loop ends when k =5
1+1=2
2+1=3
3+1=4
4+1=5 this ends the loop bc 5 is not fed as a value bc it doesn’t match the given condition.
How many times would the following loop iterate?
For m = 1 To 8 Step 3
Display m
End For
3 iterations:
m=1
sentinal = 8 step/skip 3
1+3=4
4+3=7
7+3=10
(loop ends bc 10 is not fed into the loop bc it does not meet the condition 8 limit)
If the following pseudocode were coded and executed, assuming all variables have been declared as integer, what ould be the display?
Set x = 0
For m = 1 To 3
For p=1 to 3
Set x = x + p
End For
End for
Display x
18
x=18 bc of the nested loop structure
Since the inner loop iterates 3 times for each iteration of the outer loop, and the outer loop iterates 3 times, the inner loop will execute a total of 9 times.
Each time the inner loop executes, the value of p is added to x, so after all iterations have completed, the value of x will be (1 + 2 + 3) * 3 = 18.
In the following pseudocode, which value is the accumulator?
Set x = 0
For m = 1 to 10
Set x = x + m
End For
Display x
x
accumulators are always initialized to 0 and added to the counter to keep a running total.
Select all that apply
Which of the following cause(s) a statement or set of statements to repeat as long as a condition is True?
A) Do-While loops
B) For Loops
C) While Loops
D) Do-Until Loops
A, B, C)
Do While, While, and For Loops repeat as long as the condition is true
Select all that apply.
Which of the following are posttest loops?
A) Do-Until
B) Do-While
C) For
A and B
Do until and Do While loops
A(n) _____________ structure is commonly known as a loop.
repetition structure
A(n) _______________ - controlled loop repeats a statement or set of statements a specific number of times.
count-controlled
A(n) _________ loop continues to repeat until the program is interrupted.
infinite loop
A count-controlled loop uses a variable known as a(n) ____________ to store the number of iterations that have been performed.
counter
The variable that keeps a running total in a loop is known as a(n) _______________.
accumulator
When processing a long list of values in a loop, the program knowns it has reached the end of the when the ____________ value is read.
sentinel
In a For loop, a negative step value is used to _____________ the counter variable.
decrement
In a count-controlled loop, the test action compares the counter variable to the _____________ value to determine if the loop iterates or terminates.
maximum
A loop that iterates as long as a condition is false and then stops when the condition becomes true is known as a(n) _________ loop.
Do-Until loop
*****When a(n) ___________ loop executes, the condition is tested at the beginning. If it is true, the loop body executes and the loop starts over. If it is false, the loop terminates.
**CHATGPT says While textbooks says While like wtf
A structure that has a loop inside another loop is called a(n) ____________ loop.
Nested loop
In a nested loop, the inner loop goes through all of its iterations for every iteration of the _____________ loop.
Outer loop
The total number of ___________ of a nested loop is the product of the number of all the loops.
Iterations
The ___________ statement is specifically designed to initialize, test, and increment or decrement a counter variable.
For