Loops Flashcards

1
Q

What are the 4 kinds of loops we’ve learned?

A

while, do-while, for and for-each loops

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

What is the loop body?

A

The loop body is the statements inside the loop that are executed every iteration

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

What is the difference between a while loop and a do-while loop?

A

A while loop checks the loop-continuation-condition before entering the loop body, while a do-while loop checks it after the first iteration

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

When are while and do-while loops generally used?

A

When the number of iterations is not predetermined

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

When is a for loop generally used?

A

When the loop body needs to be executed a certain number of times

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

What is the syntax of the do-while loop?

A

do {
body of the loop;
} while (testExpression)

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

What is the syntax of the for loop?

A

for (initialExpression; testExpression; updateExpression) {
//body of the loop;
}

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

What is i after the following for loop?

int y = 0;
for (int i = 0; i<10; ++i) {
y += i;
}

A. 9
B. 10
C. 11
D. undefined

A

D. undefined
Explanation: The scope of i is inside the loop. After the loop, i is not defined.

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

how would you loop through and print the numbers in this array?

int[] groupNumbers = {11, 27, 65, 82, 55};

A

for (int i = 0; i < groupNumbers.length; i++){
System.out.println(groupNumbers[i]);}
Or
For(num : groupNumbers) {
System.out.println(num);}

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

what will be the output of this for loop?
int number = 1;
for(int i = 1; i < =4; i++){
number *= i;
System.out.println(number);

A

1
2
6
24

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

when finding/comparing the length of an array for a for loop why do we use:
A. i < arrrayName.length
vs
B. 1 <= arrrayName.length

A

because indexing an array starts at the number 0, and so the last element in the array is actually located at the length - 1.

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

What is an iteration?

A

One time through the body of a loop.

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

A while loop runs until…….

A

A while loop runs until the test condition is made false.

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

What is the syntax for a while loop?

A

while (testExpression) {
//loop body
}

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

How does a while loop work?

A

1) It evaluates the boolean test expression:
a) If the value is false, the entire loop is skipped.
b) If the value is true, the the body of the loop is executed
and the loops starts over.

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

What will the difference in the output of these 2 different types of loops?
1) int i = 1;
while(i <= 3) {
System.out.println(i);
i++;
}
2) for(int i = 1; i <= 3; i++) {
System.out.println(i);
}

A

Nothing. The output for both is:
1
2
3

17
Q

When would we use a while loop instead of a for loop?

A

When the necessary number of iterations of the loop isn’t known.

18
Q

When does an infinite loop occur and what is a the usual result?

A

An infinite loop occurs when the test expression always evaluates as true. When this happens the loop repeats infinitely and the program will eventually terminate abnormally.

19
Q

What is another name for a for-each loop?

A

A for-each loop is also called an enhanced-for loop.

20
Q

Why would we use a for-each loop instead of a normal loop?

A

A for-each loop is easier to write and read/understand. Note: We also should also want to access every element of an array or ArrayList.

21
Q

What is the syntax for using a for-each loop to loop through the elements in the ArrayList flavor and print out the elements we call tastes?

A

for(String tastes : flavor) {
System.out.println(tastes);

22
Q

How do we specify the beginning, ending or increment of a for-each loop?

A

We don’t. For-each will automatically loop through the whole list.

23
Q

What would make you use a regular loop instead of a for-each loop?

A

We don’t want to access all the elements of the array/ArrayList.
We need the index values of the elements of the array/ArrayList.