LOOP CONTROL STRUCTURES

Iterative statements are used to run a particular block statements repeatedly or in other words 'from a loop.
 A loop is a part of code of a program which is executed repeatedly.
A loop is used using condition. The repetition is done until condition becomes condition true. 
A loop declaration and execution can be done in following ways.

A.Check condition to start a loop.

B.Initialize loop with declaring a variable

C.Executing statements inside loop.

D.Increment or decrement of value of a variable.

Types of looping statements
Basically, the types of looping statements depends 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.

1. 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
statements are while loop and for loop.

2. 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 must be executed atleast one time. Common example of this looping statement is a do- while loop.

For loop

1.This is an entry controlled looping statement.

2.In for loop structure, more than one variable can be initilized. 

3.One of the most important feature of this loop is that the three actions can be taken at a time such as variable initilization, condition checking and increment/decrement.

4.The for loop can be more compact and flexible than that of while and do-while loops.

5.Syntax:-
for (initialization;test-condition; increment/decrement)
{
Statements;
}
6.Example:-Program for 'for loop.

#include<stdio.h>
#include<conio.h>
void main()
{
int a 1; 
clrscr();
for (i=0; i<5; i++)
{
 printf("\n\t Nirali Prakashan"); // 5 times
}
getch();
}
Output:
Nirali Prakashan
Nirali Prakashan 
Nirali Prakashan
Nirali Prakashan 
Nirali Prakashan

Nested for Loop

One for loop can be nested within another for loop.
Syntax: for (initializing; test condition; increment/decrement)
{
statement;
}
for (initializing; test condition; increment/decrement)
{
body of inner loop;
}
statement;
}
Example:-
Program to print "Hello !".

#include<stdio.h>
 void main()
{
int i, j;
for (i=1;i<=3;i++)  /* Outer for Loop */
{
for(j=1;j<=2;j++) /* Inner for Loop */
{
printf("Hello!\n");
}
}
}
Output:
Hello!
Hello!
Hello!
Hello!
Hello! 
Hello!

while Loop

This is an entry controlled looping statement. It is used to repeat a block of statements until condition becomes true.
Syntax:
while(condition)
{
statements;
increment/decrement;
}
Example:
Program for while loop.
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
a=1;
while(a<=5)
{
printf("\n Nirali Prakashan");
a+=1; 
}
getch();
}
Output:
Nirali Prakashan
Nirali Prakashan
Nirali Prakashan
Nirali Prakashan
Nirali Prakashan

do-while Loop

This is an exit controlled looping statement.
Sometimes, there is need to execute a block of statements first then to check condition. At that time such type of a loop is used.
In do-while, block of statements are executed first and then condition is checked.
Syntex
do
{
statements;
(Increment/decrement);
}while(condition);
Example:
Program for do-while loop.
#include<stdio.h>
#include<conio.h>
void main()
{
int a; 
clrscr();
a=1;
do
{
printf("\n\t Nirali Prakashan");
 a+=1; 
}while(a<=6);
a=6;
do
{
printf("\n\n\t Pragati"); 
a+=1;
} while(a<=5);
getch();
}
Output:
Nirali Prakashan
Nirali Prakashan
Nirali Prakashan 
Nirali Prakashan
Nirali Prakashan
Pragati