Junior — Middle
Can you explain how a loop with a condition at the beginning works?
sobes.tech AI
Answer from AI
A loop with a condition at the beginning is a while loop in Python. It executes the loop body only if the condition is true, checking this condition before each iteration.
Example:
count = 0
while count < 5:
print(count)
count += 1
In this example, the condition count < 5 is checked first. If it is true, the loop body (printing and incrementing the counter) is executed. If the condition is false, the loop ends. Thus, the loop may not execute at all if the condition is initially false.