Unit 4: Iteration Flashcards
What is the purpose of a while loop?
A while loop is used when you need to iterate for an indefinite period of time until a certain condition is met.
What is the purpose of a for loop?
A for loop is used when you need to iterate a specific number of times and you know how many times you must iterate.
What is the difference between a for loop and a for each loop?
A general for loop has to set up a variable to increment, a condition to follow, and the actual increment itself
A for each loop is more useful for arrays when the index is not needed
What is a nested loop?
A loop exists inside the body of another loop
What is the correct way to create a for loop?
A. for(variable initialization, boolean expression, increment)
B. for(boolean expression; variable initialization; increment)
C. for(variable declaration : array name)
D. for(variable initialization; boolean expression; increment)
D. for(variable initialization; boolean expression; increment)
ex:
for(int i = 0; i < arr.length; i++)
How often is the inner loop of a nested loop run?
A. Infinitely
B. One more time than the outer loop
C. Each time the outer loop runs
D. Two less times than the outer loop runs
C. Each time the outer loop runs
Why do we use while loops in Java?
A. To break out of some block of code
B. To do something if a condition is true
C. To repeat some code while a condition is true
D. To repeat something for a fixed number of times
C. To repeat some code while a condition is true
Which statement can we use inside of a loop to end it? A. System.out.println; B. break; C. SENTINEL; D. else;
B. break;
Indicates the number of times a statement is executed by the program.
Statement execution count
What is an Enhanced For Loop
A loop that is an alternate to a for or while loop that accesses each value in an array starting at the first value and proceeding in order.
This error occurs when we have “<=” instead of “
“off by one” errors
structure of a while loop
while (condition) { … }