QUESTION IMAGE
Question
question: 7
we want to simulate constantly flipping a coin until we get 3 heads in a row. what kind of loop should
for loop
while loop
loop and a half
infinite loop
A While loop is used when the number of iterations is not known in advance. In this case, we don't know how many coin flips it will take to get 3 heads in a row. The loop will continue (while the condition of not having 3 heads in a row is true) until the desired outcome (3 heads in a row) is achieved. A For loop is typically used when the number of iterations is known (e.g., for i in range(10)). Loop and a half is not a standard programming loop structure. An Infinite loop (like while True) would run forever unless there is a break statement, but here we have a clear stopping condition (3 heads in a row) which a While loop can handle without being infinite (as the loop condition will eventually become false).
Snap & solve any problem in the app
Get step-by-step solutions on Sovi AI
Photo-based solutions with guided steps
Explore more problems and detailed explanations
While loop