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 nem of type int, initialized to se. Then, until nem is not equal to nemar, increment nem.

Solution

for (int nem = se; nem != nemar; nem++) {
    ...
}

Something to double-check in your solution:


Part 2

Consider the following code:

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

  2. Assume the loop ends because the test condition of the loop is false on iteration 3. Write out the the order in which the statements will execute.

Solution

  1. Order:

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

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

Related puzzles: