What four parts commonly make up an iterative algorithm?
Initialization sets starting values, the condition decides whether to continue, the body performs each iteration’s work, and the update moves the loop toward termination.
Study Java Iteration and Algorithmic Patterns with 12 free online flashcards. Review key terms, definitions, and concepts with this interactive flashcard deck.
What four parts commonly make up an iterative algorithm?
Initialization sets starting values, the condition decides whether to continue, the body performs each iteration’s work, and the update moves the loop toward termination.
Why can a while loop execute zero times?
A while loop tests its Boolean condition before each possible body execution, so a false condition initially causes zero iterations.
What is the execution order of a basic for loop?
A for loop initializes once, tests its condition, executes the body, performs its update, and then repeats the test.
What guarantees one execution of a do-while loop?
A do-while loop is a posttest loop: it runs the body first and checks the condition afterward, so the body executes at least once.
What is total after the loop adds x from 1 through 4?
The final value is 10. The loop adds 1, 2, 3, and 4 to total, then stops when x becomes 5.
How should a sentinel be handled in a sentinel-controlled loop?
A sentinel is a special value that ends processing. Test it before processing so it is not accidentally included in the calculation.
How do i < 5 and i <= 5 differ when i starts at 0?
The loop with i < 5 processes 0 through 4, for five iterations. Changing the condition to i <= 5 includes 5, making six iterations.
Why is while (x < 5) infinite when only y is decremented?
It is infinite because x controls the condition but never changes inside the loop; therefore x < 5 remains true.
How should a maximum search be initialized?
Initialize maximum with the first array element, then compare the remaining elements. Starting at 0 fails when every element is negative.
What does the digit-reversal loop produce from 472?
The algorithm extracts 2, then 7, then 4 from 472 and appends each digit to reversed, producing 274.
How many times does a 3-by-4 nested loop execute its inner statement?
The printing statement executes 12 times: the outer loop runs 3 times and the inner loop runs 4 times for each outer iteration.
How many times does the inner loop run in the four-row triangular pattern?
The inner loop runs 1 + 2 + 3 + 4 = 10 times because its bound depends on the current row.