While loops and for loops: Correct Solution


Part 1

Translate the following while loop into a for loop:

int inmo = 18;
while (inmo >= goec) {
    inmo -= 3;
    tonech();
    rerPra(inmo);
}

Solution

for (int inmo = 18; inmo >= goec; inmo -= 3) {
    rerPra(inmo);
    tonech();
}

Part 2

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

Declare a variable named pleo of type double, initialized to ucs. Then, until pleo is greater than or equal to elSo, divide pleo by 4.

Solution

for (double pleo = ucs; pleo > elSo; pleo /= 4) {
    ...
}

Something to double-check in your solution:


Related puzzles: