Loops in C

Loops in C

For loop

For loops is very similar to a while loops in that it continues to process a block of code until a statement becomes false, and everything is defined in a single line. The for loop is also entry-controlled loop.

Syntax :-

for (initialization; condition test; increment or decrement)

{

       //Statements to be executed repeatedly

}

While loop

While loop is a most basic loop in C programming. while loop has one control condition, and executes as long the condition is true.  The condition of the loop is tested before the body of the loop is executed, hence it is called an entry-controlled loop.

Syntax :-

while (condition)

{

   statement(s);

   Incrementation;

}

 

do-while loop

do-while loops are very similar to the while loops, but it always executes the code block at least once and furthermore as long as the condition remains true. This is an exit-controlled loop.

Syntax :-

do

{

   statement(s);

} while (condition);

Nesting of loop

C programming language supports nesting of one loop inside another. You can define any number of loop inside another loop. You can also have any number of nesting level. You can put any type of loop in another type. For example, you can write a for loop inside while loop, while inside another while etc.

Syntax :-

outer_loop

{

    inner_loop

    {

    // Inner loop statements

    }

    // Outer loop statements

}

NOTE :- outer_loop and inner_loop is one of the valid C loop i.e. either for loop or while loop or do...while loop.

 
This website was created for free with Own-Free-Website.com. Would you also like to have your own website?
Sign up for free