C provides three primary types of loops to perform repetitive tasks: for
, while
, and do-while
loops.
-
For Loop: The
for
loop is used when the number of iterations is known beforehand. It has three parts: initialization, condition check, and increment/decrement. Example: -
While Loop: The
while
loop repeats as long as the specified condition is true. The condition is checked before the execution of the loop’s body. Example: -
Do-While Loop: The
do-while
loop guarantees that the body of the loop is executed at least once. The condition is checked after executing the loop body. Example:
Each loop type is useful in different scenarios depending on whether the number of iterations is known in advance or whether the loop should always execute at least once.