# Looping Statements

0
1380

Introduction- In some cases it is essential to execute certain set of statements repeatedly. Certain numerical equations can be solved by iterative methods. C provides looping statements to do repeated operations. We have three types of looping statements viz. for, while and do while loops.

for() loop

```    Syntax:
for(initialization-expression;conditional-expression;update-expression)
Statement(s);```

Where initialization-expression executes only once and initializes the starting value of loop counter. Conditional-expression checks the loop counter value with the target value. When the condition is true all the statements in the body of the loop get executed and the control advances to update-expression. Update-expression either increases or decreases the value of loop counter so that we are coming nearer to the solution in each and every step. Ex:1.

```          int i;
for(i=1;i<=10;++i)
printf("%d\n",i);
It prints all numbers between 1 to 10 (1,2,3.......10)```

Now we can see the following program, which prints first n natural numbers. Here we need to take the value of ‘n’ from keyboard. The value of loop counter has to be initialized to one as natural numbers starts from one. The condition becomes i<=n which gives either true or false. Until the condition is true the body of the loop keeps on executing. Each time the update expression increases the value of loop counter by one.

while() loop

```          Syntax:
Initial-expression;
while(conditional-expression)
{
Statement(s);
Update-expression;
}```

In the for loop we have discussed earlier we know in advance how many iterations of the body of the loop we have to perform. But in some cases we do not ave any ideas about how many iterations we have to perform. In that case we use while loop. All programs that uses for loop can be solved by while loop also.

do-while loop

```             Syntax:
Initial-expression;
do
{
Statement(s);
Update-expression;
}while(condition-expression);```

It is similar to while loop, but in this the body of the loop executes first and then the condition will be checked. The do-while loop executes at least one time.

Nested Loops

We can nest one loop within another to solve more complex problems. In such cases the inner loop executes completely for each change in outer loop. Ex.

```              for(int i;i<10;i+=2)
for(int j=5;j>=3;j++)
printf("%d\t",j);
The output will be
5     4    3     5       4       3   .....     5      4     3
9 times```

For each changes in outer loop all possible changes takes place in inner loop. We can nest the loops to any extent but nesting to more depth will lead to more number of executions. Nested loops can be for loop within while loop, within for loop, do….while within for or while loop and vice versa. When we are nesting the loops we have to take care of compound statements i.e. we need to clearly mark the body of each of the nested loops.

The continue statement

This statement is used to skip certain statements within the body of loop.

```        Syntax:
continue;```

Ex: Consider printing all the number between 1 & n that are divisible by five.

```                for(i=1;i<=n;++i)
{
if(i%f!=0)
continue;
printf("%d\n",i);
}
where i&n are integers.```

Looping Statements show many looping statements are there in c Looping Statements in c looping control statements looping conditional statements looping statements in c with examples c programming looping statements while loop do while loop for loop loop statement program while loop program do while loop program for loop program