Iterative (Looping): Iterative is also known as looping. It is
used when we want to execute a statement again and again. The process of
executing a statement again and again is known as loop. Such type of statement
is known as iterative or looping statement. Loop statements are categorized in
two controls: entry control loop and exit control loop. Entry control loop is
the loop where condition is checked at entry point. If it is true then body of
loop is executed. Otherwise the control is moved out of the loop when condition
becomes false. Entry control loop is also called base loop. The while-loop and
for-loop are example of entry control loop. Exit control loop is the loop when
condition is checked at exit point. If condition is true then body of loop is
executed otherwise not. The example of exit control loop is do-while loop
statement. Each loop has the same purpose i.e. repeating the task again and
again till condition becomes false. The basic loop statements are:
while loop
do while loop
for loop
1) while loop: While loop is an entry control loop. First of all, the condition is
checked at entry point. If it is true then body of loop is executed otherwise
loop is terminated. This loop will execute again and again till condition
becomes false.
General syntax of while loop:
initialization;
while(condition)
Notes By:
Vijay Kumar
Bhargav (98728-71471)
|
statements;
incre/decre;
}
Example of while loop:
#include<stdio.h>
#include<conio.h>
void main()
{
int w;
w=0; //initialization
while(w<5)
{
printf("\n my name
is while loop"); // executable
statement
w++; //
increment
}
printf("while loop is
terminated");
getch();
}
2) do-while loop: The do while loop is an exit control loop. In this loop the condition
is checked at exit point. First of all body of loop is executed then condition
is checked. If condition is true then body is executed again, otherwise not. It
is also known as do-loop. Body of loop is executed till the condition becomes
false.
General syntax of do loop:
initialization;
do
{
statements;
incre/decre;
} while(condition);
Note that in while loop there is no semi colon at the end
but in do while there is semi colon at the end when while (condition) is
written. Example:
#include<stdio.h>
#include<conio.h>
void main()
{
int d;
d=0; //
initialization
do
{ printf("i am do while loop");
d++; //
incre/decre
} while(d<5); //
condition
printf("exited from do
while");
getch();
}
3) for loop: The for loop is an entry control loop. In this loop, condition is
checked at entry point of loop body. Every loop has three parts:
initialization, condition check and increment/decrement. In other loops all
three parts initialization, condition check and increment/decrement are written
at separate places. But in for loop, all three parts are written at same place
in single line.
General syntax of for loop:
for(initialization; condition check; incre/decre)
{
body of loop;
Notes By:
Vijay Kumar
Bhargav (98728-71471)
|
Example of for loop:
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
for(i=0;i<5;i++)
{
printf("i am for
loop");
}
getch();
}
Each loop has the same purpose i.e. repeating the task
again and again till condition becomes false.
4) nested for loop: When a for loop occurs within another for loop then it
is known as nested for loop. We can apply number of nested for loops in C
language.
General syntax of nested loop:
for(initialization; condition check; incre/decre)
{ for(initialization; condition
check; incre/decre)
{ body of inner loop;
}
body of outer loop;
}
In nested loop, inner loop will be executed first then
outer loop will be executed. Example:
#include<stdio.h>
#include<conio.h>
void main()
{ int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{ printf("this is inner loop");
}
printf("this is
outer loop");
}
printf("exit from all
loops");
getch();
}
Blog Author: Vijay Kumar
No comments:
Post a Comment