Share this post on:

Iterative statement in C is used when we want to execute a particular statement number of times on the basis of certain condition we use iterative statement also called loop.

There are three types of loops used in C Programming:

  • For Loop
  • While Loop
  • Do while Loop

For Loop : It is counter loop, useful when we know the exact number of repetition.

The general format : for(initialization; condition; increment/decrement) { body }

Example: Write a Program to print name hundred times?

#include<stdio.h>

main()
{
int i;
for(i=1;i<=100;i++)
{
printf(“C Programming”);
}
}

Example : Write a Program to print all odd number of 1 to 100?

#include<stdio.h>

main()
{
int i;
for(i=1;i<=100;i++)
{
if(i%2!=0)
{
printf(“Odd = %d”,i)
}
}
}

Example : Write a Program to print the table of 6?

#include<stdio.h>

main()
{
int i;
for(i=1;i<=10;i++)
{
printf(“%d”,(6*i));
}
}

Nested for Loop : We can write for loop inside the other for loop it is called the nested for loop. Nested for loop is basically useful to represent output in more than one direction that is now and column. In this case outer loop worked for row and inner loop worked for column.

Example : Write a program to print the following series?

1

1 2

1 2 3

1 2 3 4

#include<stdio.h>

main()
{
int i,j;
for(i=1;i<=4;i++)
{
for(j=1;j<=i;j++)
{
printf(“%d”,j)
pritnf(“\n”);
}
}
}

Example : Write a Program to print the following series?

1 2 3 4

1 2 3

1 2

1

#include<stdio.h>

main()
{
int i,j;
for(i=4;i>=1;i–)
{
for(j=1;j<=i;j++)
{
printf(“%d”,j)
pritnf(“\n”);
}
}
}

Special Situation with for loo

  • for(i=1;i<=100;i++);

printf(“%d”,i);

output = 101

if we use semicolon with the loop it means loop has no body.

  • i=1;

for(;i<=100;i++)

printf(“%d”,i);

output = 1 to 100

if we want, we can live any section of for loop as empty but we must give two semicolon.

  • i=1;

for(;i<=100;)

printf(“%d”,i++);

output = 1 to 100

  • for(i=1,j=5;i<=10;i++;j–)

printf(“%d%d”,i,j);

output = 1,5,2,4,3,3,4,2,5,1,6,6,7,-1,8,-2,-9,-3,10,-4

if we want to use write more than one expression in a single section of for loop but it must be separated by comma.

  • for(i=1;j=5;j>=0;i<=10;i++,j++)

printf(“%d%d”,i,j);

when we use more than one condition in the single section of for loop, loop will be terminated according to right hand side condition.

while loop: It is a conditional loop useful when we don’t know the exact number of repetition.

The general format is:

while (condition) { body to repeat }

Although it is condition loop but we can also used it as a counter loop in this case and increment/decrement section externally.

initialization

while(condition)

{

body to repeat increment/decrement

}

Example : Write a Program to print 1 to 100 ?

#include<stdio.h>

main()
{
int i;
i=1;
while(i<=100)
{
printf(“%d”,i);

i++;
}
}

Example : Write a program to input a number and find the smallest digit?

#include<stdio.h>

void main()
{
int num,temp,min=9;
clrscr();
printf(“Enter a no.”);
scanf(“%d”,&num);
while(num>0)
{
temp = num%100;
if(temp<min)
min=temp
max=max/10;
}
printf(“Smallest digit = %d”,min);
}

Do While Loop : When its is necessary to execute a loop at least once we use do while loop. In do while loop the body will be executed and then check the condition.

General Format:

do

{

bodies to repeat

}

while(condition);

It is also use as a counter loop.

Example : Write a Program to input a no and print the table?

#include<stdio.h>

void main()
{
int num,i;
printf(“Enter a no”);
scanf(“%d”,&num);
i=1;
do
{
printf(“%d”,(num*i));
i++;
}
while(i=10);
}

Example : Write a Program to input a no. and print the factorial?

#include<stdio.h>

void main()
{
int n,m=1;
printf(“Enter a no”);
scanf(“%d”,&n);
do
{
m=m*n;
n–;
}
while(n>=1);
printf(“%d”,m);
}

Share this post on:
Avatar Raiyan

Author: Raiyan

Hi, I guess you're here because you want to know a bit about me. huh? I am not so good on talking about myself but I'll give a short about me.

My Name is Raiyan. I am a Professional Application Developer and a Blogger.
I started this website to Share my Knowledge. Here I provide all my knowledge whatever I earned till now.

Leave a Comment

Your email address will not be published. Required fields are marked *