answersLogoWhite

0

Why you use loop in c?

User Avatar

MujeebAhmad

Lvl 1
13y ago
Updated: 8/20/2019

To iterate the same command or series of commands while a conditional expression remains true. The parameters of those commands can (optionally) be altered by each iteration of the loop, as can the parameters used in the conditional expression.

Conditional expressions may also be evaluated anywhere within the loop, forcing the loop to continue (start a new iteration, skipping any remaining commands in the loop), break (exit the loop completely and begin execution at the first command following the loop) or goto (jump to a specified label either inside or outside of the loop).

The for() loop is the most common type of loop, usually used for counting a series of values in a sequence of increments. It accepts three parameters: the start value, a conditional expression and a function. All parameters are optional, but if a conditional expression is specified it is evaluated at the end of each loop, immediately after invoking the optional command.

The while() loop must specify a conditional expression which is evaluated before each iteration of the loop.

The do...while() loop must also specify a conditional expression which is evaluated at the end of each iteration. Such loops are guaranteed to loop at least once.

All three are capable of creating infinite loops. Often this is desirable, but great care must be taken to ensure an infinite loop can exit gracefully at some point, otherwise your program will stall. The worst-case scenario is that the loop could bring the entire system to a halt either because the loop continually allocates memory or makes recursive calls to the function containing the loop. Infinite loop must therefore contain a conditional expression to exit the loop from within the body of the loop itself.

User Avatar

Wiki User

13y ago

What else can I help you with?