While loops and for loops: Correct Solution


Part 1

Translate the following while loop into a for loop:

int um = 76;
while (um < laMa) {
    um /= 4;
    sciol(um, 37);
}

Solution

for (int um = 76; um < laMa; um /= 4) {
    sciol(um, 37);
}

Part 2

Consider the following code:

A
B
while (C) {
    D
    E
    if (F) {
        G
        break;
    }
    H
    I
}
J
K
L
  1. Assume the loop breaks on iteration 1. Write out the the order in which the statements will execute.

  2. Assume the loop breaks on iteration 3. Write out the the order in which the statements will execute.

Solution

  1. Order:

    A B C D E F J K L
  2. Order:

    A B C D E F G H I C D E F G H I C D E F J K L

Related puzzles: