187,878 views
18 votes
18 votes
You are creating a game in which an enemy sprite performs a certain action

five times and then stops. Which type of loop would be the best choice for
this function?
A. An infinite loop
B. A while loop
C. A repeat loop
D. A forever loop

User Dorji Tshering
by
2.8k points

2 Answers

24 votes
24 votes

Final answer:

A repeat loop is the most suitable choice for repeating an action a specific number of times, such as five times in a game, and then stopping.

Step-by-step explanation:

The best choice for a loop where an action needs to be repeated a specific number of times, in this case, five times, would be a repeat loop.

This loop allows you to specify how many times the action should be carried out, stopping automatically once the limit is reached. Infinite and forever loops continue endlessly, making them inappropriate for this situation, and while loops would work but are less succinct since they require a manually incrementing condition.

User Jeswang
by
2.7k points
24 votes
24 votes

Final answer:

The best choice for a game in which an enemy sprite performs a certain action five times and then stops would be a repeat loop option C is correct.

Step-by-step explanation:

The best choice for a game in which an enemy sprite performs a certain action five times and then stops would be a repeat loop.

A repeat loop allows you to specify the number of iterations or cycles that need to be executed. In this case, you would set the repeat loop to run five times, and after the fifth iteration, the loop would stop.

For example, in Python, you can use a for loop with the range() function to achieve this:

for i in range(5):
# perform the action here

User Stukennedy
by
3.4k points