C Language Loop Statements

C Language Loop Statements

Loop statements:

The looping statements are used to execute group of statements repeatedly until some condition is satisfied.

Types of looping statements:

Basically, the types of looping statements depend on the condition checking mode. Condition checking can be made in two ways as: Before loop and after loop. So, there are two types of looping statements.

•    Entry controlled loop

•    Exit controlled loop

Entry controlled loop:

In such type of loop, the test condition is checked first before the loop is executed. Some common examples of this looping statement are:

•    while loop

•    for loop

Exit controlled loop:

In such type of loop, the loop is executed first. Then condition is checked after block of statements are executed. The loop executed at least one time compulsorily. Some common example of this looping statement is:

•    do-while loop

A loop basically consists of a loop variable which has for the completion of any iterative   statement, i.e. a loop consists of three main statements:-

•    Initialization

•    Condition

•    Incrementation
        Initialization is used to start any loop i.e. it gives some initial value to the loop variable. This defines that from which value the loop has to get start. Condition is provided to give the final value to the loop variable so that how many times the loop has to get executed. For reaching the loop variable from initial value to the final value there should be some sort of incrementation and that provided by the third component statement of loop and that is incrementation. If in any loop the incrementation or the final value is not provided then the loop becomes infinite. If the initialization is not done then the garbage value of the loop variable becomes its initial value.

While loop:

The while statement is used to carry out looping operations, in which a group of statements are executed repeatedly, until some condition is satisfied.
This is used to execute a set of statements repeatedly as long as the condition is TRUE. The syntax of While Loop is:

 while (condition) 
         {
   statement;
         }
Where,
Statement is simple or compound statement
While is keyword
Condition is results in TRUE or FALSE
If the logical condition is TRUE then the statement is repeatedly executed. If the result is FALSE then control comes out of the loop and continues with the next executable statement. The value of variable involved in the logical condition will be changed during every pass of while loop. If the value does not changed then control does not come out of the loop and statements executed repeatedly that is infinite loop.

Example:
#include<stdio.h>
int main()
{
int a=0;
while (a<5)
{
printf (" the value of a is=%d/n”, a);
a++;
}

Do-while Loop:

This is used to execute a set of statements repeatedly, untill the logical test results in FALSE. The syntax of Do-while Loop is:


 do
  {
  statement;
  }
 while (condition);

 If the logical condition is TRUE then the statement is repeatedly executed. If the result is FALSE then control comes out of the do-while loop and continues with the next executable statement.

Example:

#include<stdio.h>
int main ()
{
int digit=0;
do
printf (“%d\n”, digit++);
while (digit<=9);
}

For Loop:

It is used when the programmer knows how many times a set of statements are to be executed. The syntax of for loop is:


 for ( exp1; exp2; exp3) 
 {
 statement;
 }
Where,

Exp1 is assigning the value to variable.

Exp2 is give condition to variable.

Exp3 is increment or decrement in the value of variable.
Statement is compound or simple statement
for is keyword.

Example:

#include<stdio.h>
int main(){
int i;
for (i=0; i<=4;i++){
 printf ("%d ",i);
}
return 0;    
}  

For each loop:

Loops, like for, while can be nested, one within another. The inner and outer loops need not be generated by the same type of control structure. It is essential, however, that one loop be completely embedded within the other-there can be no overlap. Each loop must be controlled by a different index.

Example:
#include <stdio.h>
int main()
{
int i, j, k;
for (i = 0; i < 3; i++)
{
for (j = 0; j < 26; j++)
{
printf ("%d",  i+ j);
}
}
 return 0;
}

Break statement:

The break statement is used to terminate loops or to exit from a switch. It can be used within a for, while, do-while, or switch statement.
Syntax: break;

Example:

#include<stdio.h>
int main ()
{
int i;
while (i < 10)
{
i++;
if (i == 5)
break;
}
return 0;
}

Continue statement:

The continue statement is used to bypass the remainder of the current pass through a loop. The loop does not terminate when a continue statement is encountered. Rather, the remaining loop statements are skipped and the computation proceeds directly to the next pass through the loop.

Syntax: continue;

Example:
#include<stdio.h>
int main()
{
int i;
while (i < 10)
{
i++;
continue;
printf ("Hello\n");
}
return 0;
}

0 comments:

Post a Comment