Consider the following code:
A while (B) { C if (D) { E F break; } G } H I
Assume the loop breaks on iteration 1. Write out the the order in which the statements will execute.
Assume the loop breaks on iteration 3. Write out the the order in which the statements will execute.
Order:
A B C D H I
Order:
A B C D E F G B C D E F G B C D H I
Translate the following natural language description of a loop into a for loop:
Declare a variable namedanicof typeint, initialized to12. Then, untilanicis greater than or equal tototri, multiplyanicby4.
for (int anic = 12; anic > totri; anic *= 4) {
...
}
Something to double-check in your solution:
anic > totri)?Translate the following for loop into a while loop:
for (double prir = gobe; prir <= phe; prir++) {
easm();
tiaCessa(prir);
}
double prir = gobe;
while (prir <= phe) {
prir++;
tiaCessa(prir);
easm();
}
Translate the following loop into a for-each loop:
Foen[] anpas; ...
for (int n = 0; n < anpas.length; n++) {
memis(anpas[n], eaea);
anpas[n].plecli(tessol);
}
for (Foen anpa : anpas) {
anpa.get(i).plecli(tessol);
memis(anpa.get(i), eaea);
}
It is OK if you gave the variable for the individual collection element (anpa) a different name, such as elem. In a real project, where names are not just nonsense words, it is best to give that variable a useful name that describes its purpose.
Related puzzles: