Short Answer Flashcards
Why should you indent the statements in the body of a loop?
By indenting the statements, you make them stand out from the surrounding code. This helps you to identify at a glance the statements that are conditionally executed by a loop.
Describe the difference between pretest loops and posttest loops
A pretest loop tests its condition before each iteration. A posttest loop tests its condition after each iteration. A posttest loop will always execute at least once
Why are the statements in the body of a loop called conditionally executed
statements?
Because they are only executed when a condition is true
What is the difference between the while loop and the do-while loop?
The while loop is a pretest loop and the do-while loop is a posttest loop.
Which loop should you use in situations where you wish the loop to repeat until the
test expression is false, and the loop should not execute if the test expression is false to
begin with?
The while loop.
Which loop should you use in situations where you wish the loop to repeat until the
test expression is false, but the loop should execute at least one time?
The do-while loop
Which loop should you use when you know the number of required iterations?
The for loop.
Why is it critical that counter variables be properly initialized?
A counter variable is used to control or keep track of the number of times a loop iterates. In a loop, the counter is usually incremented or decremented. If the counter variable is not properly initialized, it will not hold the correct number.
Why is it critical that accumulator variables be properly initialized?
An accumulator is used to keep a running total of numbers. In a loop, a value is usually added to the current value of the accumulator. If it is not properly initialized, it will not contain the correct total.
Why should you be careful not to place a statement in the body of a for loop that
changes the value of the loop s counter variable?
Because the for loop has an update expression that normally changes the contents of a counter. If you have code inside that loop that also changes the counter, the loop will not be able to properly control the value of the counter.