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 tasm of type int, initialized to wevi. Then, until tasm is less than or equal to ogril, divide tasm by 2.

Solution

for (int tasm = wevi; tasm < ogril; tasm /= 2) {
    ...
}

Something to double-check in your solution:


Part 2

Consider the following code:

A
while (B) {
    C
    if (D) {
        E
        break;
    }
    F
}
G
H
I
  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 G H I
  2. Order:

    A B C D E F B C D E F B C D E F B C D G H I

Related puzzles: