228k views
4 votes
Which type of loop in programming repeats a block of code once, and then repeats the loop as long as the specified condition is true?

A) while
B) do-while
C) for
D) foreach

1 Answer

6 votes

Final answer:

The do-while loop is used in programming to execute a block of code once and then continue executing as long as a specified condition remains true.

Step-by-step explanation:

The type of loop in programming that executes a block of code once and then repeats the loop as long as the specified condition is true is known as a do-while loop. Unlike the while loop, which evaluates its condition at the beginning of each iteration, the do-while loop ensures that the block of code is executed at least once, because the condition is checked after the code rauns. In contrast, the for loop is used when you know in advance how many times you want the loop to iterate, and foreach is typically used to iterate over collections such as arrays or lists.

User Poisson
by
7.6k points