Loops In C Programming Language

Programming

Loops are statements that execute repeatedly until the condition is satisfied. if the condition is satisfied then and then the body of the loop will be executed if the condition is not satisfied then the control is transferred to the out of the loop.

Parts of loop

There are generally three parts of the looping statement available in C programming such as initialization, condition, and iterative statement.

Initialization: it refers to where to start our program. it was done out of the loop and before the condition. it executes only a single time in a particular loop.

Condition: it refers to where to stop. it defines after the initializations if the condition is satisfied or true then the body of the loop will be executed.

Iterative  Statement: it refers to the difference increment and decrement or steps of a particular problem. it must be defined in the body of the loop.

Type of loops

Types of loops

There are two types of looping statement are there entry control loop and exit control loop.

Entry control loop:  in a loop where condition check first if the condition is satisfied then the body of the part 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 a loop.

Syntax Flowchart of Entry Control loops:

Entry Control Syntax

Example Flowchart of Entry Control loops:

Entry Control Loops

while loop:

Syntax:

Initialisation

while(condition)

{

//statement

Iterative statement //body of loop

}

Example:

int i=1;

while (1<=10)

{

printf(“%d”,i);

i++;

}

output: 1 2 3 4 5 6 7 8 9 10

as per the above syntax while is a keyword that defines a looping statement. while statement must have conditional expression one or more and it does not end with a semicolon ;. if the condition is satisfied then the body of the loop will be executed. body of the loop contains one or more statements.

as per flowchart if the condition is satisfied then it executes the body of the loop, in the body of the loop iterative statement executed and control transfer to the condition statement. body of the loop will be executed again and again until the condition is satisfied.

if the body of the loop has only a single statement then the scope is not required but if there is more than one statement then the scope is required.

for loop:

Syntax:

for(initialisation; condition; iterative statement)

{

//body of loop

}

or

for( ; condition ; )

{

//body of the loop

}

Example:

for(i=1; i<=10; i++)

printf (“%d”,i);

as per the above syntax for is a keyword that defines a looping statement. for loop statement does not end with a semicolon ;. it is the easiest loop because all parts of the loop are combined in a single statement separated by a semicolon ;. in this loop initialization is done only a single time if the condition is satisfied then the body of the loop will be executed. if the condition is false then control goes the rest of the program. it may be nested for loop but the hole loop divided into three parts. only it has exact to the semicolon.

Exit control loop: looping statement with check the condition at the end of the loop is known as an exit control loop. it makes sure that a minimum one-time body of the loop will be executed if the condition is false. it is also known as a posttest.

Syntax Flowchart of Exit Control loop:

Example Flowchart of Exit Control loop:

Exit Control Loops

do while loop:

syntax:

initialisation

do

{

//body of the loop

iterative statement

}while(condition);

Example:

int i=1;

do

{

printf (“%d”,i);

i++;

} while (i<=10);

as per above syntax do while is a keyword which considered as a looping statement. do while must end with semi colon ;.

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