Unit 4: Iteration Flashcards

1
Q

What is the purpose of a while loop?

A

A while loop is used when you need to iterate for an indefinite period of time until a certain condition is met.

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

What is the purpose of a for loop?

A

A for loop is used when you need to iterate a specific number of times and you know how many times you must iterate.

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

What is the difference between a for loop and a for each loop?

A

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

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

What is a nested loop?

A

A loop exists inside the body of another loop

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

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)

A

D. for(variable initialization; boolean expression; increment)

ex:
for(int i = 0; i < arr.length; i++)

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

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

A

C. Each time the outer loop runs

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

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

A

C. To repeat some code while a condition is true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
Which statement can we use inside of a loop to end it?
A. System.out.println;
B. break;
C. SENTINEL;
D. else;
A

B. break;

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

Indicates the number of times a statement is executed by the program.

A

Statement execution count

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

What is an Enhanced For Loop

A

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.

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

This error occurs when we have “<=” instead of “

A

“off by one” errors

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

structure of a while loop

A

while (condition) { … }

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