C Programming + Tutorials in C for Beginners

C Looping Statements

Home  »  C Programming  »  C Looping Statements


When a single statement or a group of statements will be executed again and again in a program (in an iterative way), then such type processing is called loop. Loop is divided into two parts:

(a) Body of the loop
(b) Control of loop

The looping statements used in C-language are :

a) while statement or while loop
b) do statement or do loop
c) for statement or for loop
d) Nested for loop statement




while Statement or while loop

     While statement or while loop is an entry control loop. In this, first of all condition is checked and if it is true, then group of statements or body of loop is executed. It will execute again and again till condition becomes false.

The general syntax is :
while (test condition)
{
   block of statements;
}
statements-x;




Output is as :
I=1
I=2
I=3
I=4
I=5
I=6
I=7
I=8
I=9
I=10







do Statement or do loop

     Do statement is exit control loop. It is also called do-while statement. In this statement, first body of the loop is executed and then the condition is checked. If condition is true, then the body of the loop is executed. When condition becomes false, then it will exit from the loop.

Note : do while loop always give one output although given condition is true or false.

The syntax of do-loop is as follow:
do
{
    block of statements;
}
while (condition);
statement-x;




Output is as :
I=1
I=2
I=3
I=4
I=5
I=6
I=7
I=8
I=9
I=10







for Statement or for loop

     It is a looping statement, which repeat again and again till it satisfies the defined condtion. It is one step loop, which initialize, check the condition and increment / decrement the step in the loop in a single statement.

The general syntax is as :
for(initial value; test condition; increment/decrement)
{
    body of the loop;
}
statement-x;




Output is as :
I=1
I=2
I=3
I=4
I=5
I=6
I=7
I=8
I=9
I=10







Nested for loop statement

     When a for statment is executed within another for statement, then it is called nested for statement. We can apply number of nested for statement in C-Language.

The general syntax is as : for(initial value1; test condition1; increment/decrement1)
{
  for(initial value2; test condition2; increment/decrement2)
  {
      inner-body of the loop;
  }
  outer-body of the loop;
  statement-x;
}




Output is as :
*
**
***
****
*****



<< Previous Topic
Next Topic >>



Previous
Next Post »
Thanks for your comment