Sovi.AI - AI Math Tutor

Scan to solve math questions

QUESTION IMAGE

quiz 3 - loops the code below was rejected by a lead programmer: n = 0 …

Question

quiz 3 - loops
the code below was rejected by a lead programmer:
n = 0
while (n < 10)
display(\woo\)
n = n - 1
why do you think this code was rejected?

Explanation:

Brief Explanations

The code has a logical error in the increment operation. In the line n = n - 1, instead of incrementing n (which is needed to eventually exit the loop when n < 10 is no longer true), it is decrementing n. So the loop will either run infinitely (if the language allows n to go into negative values without bounds) or until some system - imposed limit on negative values is reached, and will not achieve the likely intended goal of looping 10 times.

Answer:

The n = n - 1 statement decrements n instead of incrementing it, which may lead to an infinite loop or incorrect loop behavior.