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 be of type int, initialized to ent. Then, until be is less than sce, multiply be by 4.

Solution

for (int be = ent; be <= sce; be *= 4) {
    ...
}

Something to double-check in your solution:


Part 2

Consider the following code:

A
B
C
while (D) {
    E
    F
    if (G) {
        H
        break;
    }
    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 4. Write out the the order in which the statements will execute.

Solution

  1. Order:

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

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

Part 3

Translate the following while loop into a for loop:

int he = pa;
while (he <= ioci) {
    he--;
    egaGai(he);
}

Solution

for (int he = pa; he <= ioci; he--) {
    egaGai(he);
}

Related puzzles: