Entry control and exit control loop in c language
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.
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.
- while loop :
//statement body of the loop
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.
output 1 2 3 4 5 6 7 8 9 10
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 :
for (initialization; condition; iterative statement)
// body of the loop
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
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.
//body of the loop
As per above syntax 'do while' is a Keyword which consider as a looping statement, 'do while' must end with semicolon(;).
} while (i<=10);
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.