LOOP CONTROL STATEMENTS

Loop:- The process of repeated executing a block of statements is called loop. C supports three types of looping statements.  They are…

  • While Loop
  • do-while loop and
  • for-loop            

Any loop has three things. They are…

  • Initialize the index
  • Test condition
  • Update the index

1. while loop:- It is a conditional control loop statement in C language.

Syntax:- while(test condition)

                              {

                                Statements;

                                }

First the test condition is evaluated and if it is true then the statement block will be executed.  After the execution of statements the test condition is evaluated once again.

Then if it is true the statement block will be executed continuous until the test condition finally becomes false.

Program: Write a program to display natural numbers from 1 to given number using 'While Loop'
#include<stdio.h>
#include<conio.h>
void main()
{
  int n,i=1;
  clrscr();
  printf("Enter no of elements:");
  scanf("%d",&n);
  printf("Natural Numbers from 1 to %d:\n",n);
  while (i<=n)
   {
      printf("%d\t",i);
      i++;
   }
  getch();
}

2. do-while loop:- It is an alternative form of while loop.  The only difference between while and do while is the minimum number of execution of ‘while’ is ‘0’.  For minimum no of execution of ‘do-while’ is ‘1”.

Syntax:-
do
{
  Statements;
}
While(test condition);

First the statement block will be executed and then test condition will be evaluated.  If the condition is true then the statement block will be executed once again.  This process of repeated executed continuous until  the test condition finally becomes false

Program: Write a program to accept and display natural numbers, from one to given number using do-while loop
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i=1;
clrscr();
printf(“Enter any number:”);
scanf(“%d”,&n);
printf(“Natural numbers form 1 to %d:\n”,n);
do
{
printf(“%d\t”,i);
i++;
}
while(i<=n);
getch();
}

3. for loop:- It is the most commonly used loop statement in C language. It consisting of three expressions.

Syntax:-
for(exp1; exp2; exp3)
{
Statements;
}

The first expression is used to the initialize the index, second expression is used to check whether the loop is to be continued or not, third expression is used to change the index for further iteration (increment or decrement).

For Loop

Program: Write a program to accept any number and check whether the given number is perfect or not.
*sum of factors except that number is called perfect number
#include<stdio.h>
#include<conio.h>
void main()
{
int n,sum=0,i;
clrscr();
printf(“Enter any number:”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
if(n%i==0)
sum=sum+i;
}
if(n==sum)
printf(“Given number is perfect”);
else
printf(“Given number is not pefect “);
getch();
}

Nested loops:-  Using a loop statement within another loop is called Nested loop.

Program: Write a program to display palindrome numbers from 1 to given number.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,m,i,rev;
clrscr();
printf("Enter any number:");
scanf("%d",&n);
printf("Palindrome number from 1 to %d:\n",n);
for(i=1;i<=n;i++) 
 { 
   m=i; 
   rev=0; 
   while(m>0)
   {
     rev=rev*10+(m%10);
     m=m/10;
   }
 if(rev==i)
   printf("%d\t",i);
 }
getch();
}

Program: Write a program to generate the following pattern    

1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j;
clrscr();
printf("Enter any number:");
scanf("%d",&n);
for(i=1;i<=n;i++)
  {
    for(j=1;j<=i;j++)
    {
       printf("%3d",i);
    }
  printf("\n\n");
  }
getch();
}

                       

Follow Us On