C Programming Language


Loops in C


 

While Loop :

A while loop, the mechanism for repeating a set of statements allows execution to continue for as long as a specified logical expression evaluates to true

Syntax :

while(condition)
{
    statements;
}
Example :

Output :

value of x : 15
value of x : 16
value of x : 17
value of x : 18
value of x : 19

 

for Loop :

The for loop operation is controlled by what appears between the parentheses that follow the keyword for. This is illustrated in Figure. The action that you want to repeat each time the loop repeats is the block containing the statement that calls printf(). Because you have just a single statement here, you could omit the braces. Figure shows the three control expressions that are separated by semicolons. These expressions control the operation of the loop.

for loop structure
Example :

Output :

value of x : 15
value of x : 16
value of x : 17
value of x : 18
value of x : 19

 

do While Loop :

A do...while loop is similar to a while loop, except the fact that it is guaranteed to execute at least one time.

Syntax :

do
{
    statements;
}while(condition)
Example :

Output :

value of x : 15
value of x : 16
value of x : 17
value of x : 18
value of x : 19