While loops and for loops: Correct Solution


Part 1

Translate the following while loop into a for loop:

long pi = 52;
while (pi != deph) {
    pi++;
    patres(pi);
}

Solution

for (long pi = 52; pi != deph; pi++) {
    patres(pi);
}

Part 2

Translate the following natural language description of a loop into a for loop:

Declare a variable named de of type int, initialized to 15. Then, until de is less than bil, decrement de.

Solution

for (int de = 15; de <= bil; de--) {
    ...
}

Something to double-check in your solution:


Related puzzles: