When is a `while` loop used in C?

When is a while loop used in C?

a) When the number of iterations is unknown
b) When you want to run code once
c) To define constants
d) To declare variables

Answer:

a) When the number of iterations is unknown

Explanation:

The while loop in C is used when the number of iterations is not known in advance. It repeatedly executes a block of code as long as the specified condition remains true. The condition is evaluated before the loop’s body is executed, meaning that if the condition is false from the start, the loop body may not execute at all.

While loops are useful for situations where you need to loop until a particular condition changes during runtime, such as waiting for user input or processing data until a certain state is reached.

Reference links:

https://www.rameshfadatare.com/learn-c-programming/

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top