While loops and for loops: Correct Solution


Part 1

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

Declare a variable named na of type int, initialized to 48. Then, until na is not equal to cokod, divide na by 2.

Solution

for (int na = 48; na != cokod; na /= 2) {
    ...
}

Something to double-check in your solution:


Part 2

Translate the following while loop into a for loop:

int phra = fiss;
while (phra != licir) {
    phra++;
    rirco(phra);
    etscis();
}

Solution

for (int phra = fiss; phra != licir; phra++) {
    etscis();
    rirco(phra);
}

Related puzzles: