loop in c

Entry control and exit control loop in c language

loop :
      Loop is a statement which execute repeatedly until a condition is satisfied.
      if condition is satisfied then and then body of the loop will be executed, if condition is not satisfied then control is transferred to the out of loop.

parts of loop :
      There are generally three parts of looping statement such as a initialization, conditions, iterative statement.

1. initialization :
      It refer where to start over problem, it done out of the loop and before the condition.
      it execute only single time in a particular loop.

2. condition :
      It refers where to stop also define after the initialization, if condition is satisfied or true then body of the loop will be executed.

3. iterative statement :
      This statement refers increment or decrement it must be defined in Body of the loop.

types of loop :
      There are two types of looping statement are there.
1. entry control loop
2. exit control looptypes of loop in c

1. entry control loop :
      In a loop where condition check first if condition is satisfied then body of the parts will be executed otherwise not.
it is also known as a pretest.

      There are two types of entry control loop such as a 'while' loop and 'for' loop.

  1. while loop :


syntax :


initialization
while (condition)
{
//statement body of the loop
iterative statements
}

entry control loop in c

     As per flow chart if condition is satisfied then it execute body of the loop.
     in Body of the loop iterative statement execute and control transfer to the condition statement.
     body of the loop will execute again and again until condition is satisfied.
     if condition is not satisfied then control transfer to the false path and execute rest of the program.
     If body of the loop have only single statement then scope is not required but if there are more than one statement then scope is required.

example :

int i;
i=1;
while (i<=10)
{
printf ("%d",i);
i++;
}

output 1 2 3 4 5 6 7 8 9 10

pretest loop in c

      As per above syntax 'while' is a Keyword which define looping statement. while statement must have a conditional expression one or more and it does not end with semicolon (;).
if condition is satisfied then body of the loop will be executed, body of the loop contain one or more statement.

2. for loop :


syntax :


for (initialization; condition; iterative statement)
{
// body of the loop
}

or

for ( ; condition ; )
{
//body of loop
}

      As per above Syntax 'for' is a Keyword which define a looping statement, 'for' statement does not end with semicolon(;).
      it is easiest loop because all part of the loop are combined in a single statement and separated by semicolon(;). In this loop initialization is done only single time if condition is satisfied then body of the loop will be executed. if condition is false then control goes rest of the program.
      it may be nested 'for' loop but whole look divide into 3 parts only. it has exact 2 semicolon but we want to write more than one statement which can be done with the help of semicolon(;).

nested 'for' loop

for (i=1;i<=5;i++)
{
for (j=1;j<i;j++)
{
printf ("%d",j);
}
printf ("\n");
}

for (;i<=10;i++,j++)

2. exit control loop :
      A looping statement which check the condition at the end of the loop is known as exit control loop. it make sure that minimum body of the loop will be execute if condition is false.
it is also known as a post test.


      Do while : is known as exit control loop.

syntax :

initialization
do
{
//body of the loop
iterative statement
}while (condition);

exit control loop in c

      As per above syntax 'do while' is a Keyword which consider as a looping statement, 'do while' must end with semicolon(;).

example :

int i;
i=1;
do
{
printf ("%d",i);
i++;
} while (i<=10);

do while loop in c

      As per above example i is initialized by 1 then body of the loop will be executed. if condition is satisfied then control transfer to the body of the loop this process will execute again and again until condition is satisfied.
      it must execute single time where condition is false.
generally used in the menu based programming.

« operators in c programming

Learn C programming language