Loops Flashcards
What are the 4 kinds of loops we’ve learned?
while, do-while, for and for-each loops
What is the loop body?
The loop body is the statements inside the loop that are executed every iteration
What is the difference between a while loop and a do-while loop?
A while loop checks the loop-continuation-condition before entering the loop body, while a do-while loop checks it after the first iteration
When are while and do-while loops generally used?
When the number of iterations is not predetermined
When is a for loop generally used?
When the loop body needs to be executed a certain number of times
What is the syntax of the do-while loop?
do {
body of the loop;
} while (testExpression)
What is the syntax of the for loop?
for (initialExpression; testExpression; updateExpression) {
//body of the loop;
}
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
D. undefined
Explanation: The scope of i is inside the loop. After the loop, i is not defined.
how would you loop through and print the numbers in this array?
int[] groupNumbers = {11, 27, 65, 82, 55};
for (int i = 0; i < groupNumbers.length; i++){
System.out.println(groupNumbers[i]);}
Or
For(num : groupNumbers) {
System.out.println(num);}
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);
1
2
6
24
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
because indexing an array starts at the number 0, and so the last element in the array is actually located at the length - 1.
What is an iteration?
One time through the body of a loop.
A while loop runs until…….
A while loop runs until the test condition is made false.
What is the syntax for a while loop?
while (testExpression) {
//loop body
}
How does a while loop work?
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.
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);
}
Nothing. The output for both is:
1
2
3
When would we use a while loop instead of a for loop?
When the necessary number of iterations of the loop isn’t known.
When does an infinite loop occur and what is a the usual result?
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.
What is another name for a for-each loop?
A for-each loop is also called an enhanced-for loop.
Why would we use a for-each loop instead of a normal loop?
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.
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?
for(String tastes : flavor) {
System.out.println(tastes);
How do we specify the beginning, ending or increment of a for-each loop?
We don’t. For-each will automatically loop through the whole list.
What would make you use a regular loop instead of a for-each loop?
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.