Loops Flashcards
Learn all about loops
1
Q
What two main categories of loops are there?
A
Pre-test and post-test.
2
Q
Which loop constructs (in Java) exist for doing a post-test loop?
A
Do while loop. do { // Do stuff. } while(condition);
3
Q
What loop constructs (in Java) exist to perform a pre-test loop?
A
The while and for loops.
while(condition) { }
for(initialize; condition; update) { }
4
Q
What would be printed?
for(int i = 0; i < 9; i++) {
print(i + “, “);
}
A
“0, 1, 2, 3, 4, 5, 6, 7, 8, “
5
Q
What are the values of the variables after the loop? int x = 3, y = 0; while(x % 5 != 0) { y += x++; }
A
y = 7 x = 5
6
Q
What are the values of the variables after the loop?
int qq = 16;
while(qq > 3)
qq–;
A
qq = 3
7
Q
What are the values of the variables after the loop? int x = 5, y = 15; while(x != y) { x++; y--; }
A
x and y = 10
8
Q
What are the values of the variables after the loop? int x = 4, y = 15; while(x != y) { x++; y--; }
A
There is no after the loop. This loop will run infinitely.