Sovi.AI - AI Math Tutor

Scan to solve math questions

QUESTION IMAGE

which code example is an infinite loop? loopnum = 1 while loopnum<=5: p…

Question

which code example is an infinite loop? loopnum = 1 while loopnum<=5: print(loopnum) loopnum = loopnum + 1 loopnum = 1 for loopnum in range(0,5): print(loopnum) loopnum +=1 loopnum = 1 while loopnum<=5: print(loopnum) loopnum = 1 while loopnum<=5: print(loopnum)

Explanation:

Step1: Analyze first option

The for loop for loopNum in range(0,5) will iterate 5 times as range(0,5) generates a sequence of numbers from 0 to 4. It is not an infinite - loop.

Step2: Analyze second option

The while loop while loopNum<=5: has loopNum incremented by 1 (loopNum = loopNum + 1) inside the loop. So it will iterate 5 times starting from loopNum = 1 until loopNum reaches 6 and the condition loopNum<=5 fails. It is not an infinite - loop.

Step3: Analyze third option

The while loop while loopNum<=5: has no code to change the value of loopNum inside the loop. So if loopNum starts as 1, the condition loopNum<=5 will always be true (assuming no external changes), making it an infinite - loop.

Step4: Analyze fourth option

The while loop while loopNum<=5: has loopNum incremented by 1 (loopNum += 1) inside the loop. So it will iterate 5 times starting from loopNum = 1 until loopNum reaches 6 and the condition loopNum<=5 fails. It is not an infinite - loop.

Answer:

The code `loopNum = 1
while loopNum<=5:
print(loopNum)` is an infinite loop.