Question:

What output will be produced by the code below?

var i = 1

mainLoop: repeat {
    i += 2

    switch i % 2 {
    case 0:
        break mainLoop
    default:
        break
    }
} while true

print("Complete!")

Correct answer: This code will compile but crash.

Explanation: The main repeat loop is set to loop forever, or at least until someone calls break on it. In the code, this happens only when i % 2 is 0, i.e. when it's an even number, which can never happen because we start with 1 and also add 2. So, even though it will take a long time, the i variable will eventually overflow and trigger a crash.


Keywords:

© 2017 QuizBucket.org