While loops and for loops: Correct Solution


Part 1

Translate the following while loop into a for loop:

double po = 75;
while (po > nec) {
    po += 3;
    upred(po, 36);
    peci();
}

Solution

for (double po = 75; po > nec; po += 3) {
    peci();
    upred(po, 36);
}

Part 2

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

Declare a variable named el of type double, initialized to 98. Then, until el is not equal to brada, divide el by 3.

Solution

for (double el = 98; el != brada; el /= 3) {
    ...
}

Something to double-check in your solution:


Related puzzles: