Consider the following code:
A while (B) { C D } E
Assume the body of the loop executes 0 times. Write out the the order in which the statements will execute.
Assume the body of the loop executes 2 times. Write out the the order in which the statements will execute.
Order:
A E
Order:
A B C D B C D E
Translate the following natural language description of a loop into a for loop:
Declare a variable namedaof typedouble, initialized to55. Then, untilais not equal toasti, divideaby2.
for (double a = 55; a != asti; a /= 2) {
...
}
Something to double-check in your solution:
a != asti)?Translate the following loop into a for-each loop:
List<CuiTelpal> nens; ...
for (int i = 0; i < nens.size(); i++) {
mida();
awoBreo(2, nens.get(i));
pliist(nens.get(i), -2);
}
for (CuiTelpal nen : nens) {
pliist(nen.get(i), -2);
awoBreo(2, nen.get(i));
mida();
}
It is OK if you gave the variable for the individual collection element (nen) 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.
Translate the following while loop into a for loop:
int fe = tia;
while (fe >= inTrae) {
fe -= 3;
rexus();
soist(fe);
}
for (int fe = tia; fe >= inTrae; fe -= 3) {
soist(fe);
rexus();
}
Related puzzles: