KEMBAR78
3.loop Control Statements | PDF | Control Flow | Software Engineering
0% found this document useful (0 votes)
24 views8 pages

3.loop Control Statements

The document discusses loop control statements in C programming, explaining entry-controlled and exit-controlled loops. It details the syntax and usage of 'while', 'for', and 'do' statements, providing examples for each. Additionally, it includes various programming exercises demonstrating the implementation of loops for tasks such as printing series, calculating factorials, and checking for prime numbers.

Uploaded by

Abadullah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views8 pages

3.loop Control Statements

The document discusses loop control statements in C programming, explaining entry-controlled and exit-controlled loops. It details the syntax and usage of 'while', 'for', and 'do' statements, providing examples for each. Additionally, it includes various programming exercises demonstrating the implementation of loops for tasks such as printing series, calculating factorials, and checking for prime numbers.

Uploaded by

Abadullah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 8

Loop Control Statements

In looping, a sequence of statements are executed until some conditions for termination
of the loop are satisfied. Depending on the position of the control statement in the loop, a
control structure may be classified either as the entry-controlled loop or as the exit-
controlled loop.
In the entry controlled loop, the control conditions are tested before the start of
the loop execution. If the conditions are not satisfied, then the body of loop will not be
executed.

Exit-controlled loop -> In the case of an exit-controlled loop, the test is performed at the
end of the body of the loop and therefore the body is executed unconditionally for the
first time.
The entry-controlled and exit-controlled loops are known as pre-test and post-test
loops respectively.

Loop :- A loop is defined as a block of statements which are repeatedly executed for
certain number of times.

C language provides for three constructs for performing loop operations :-

1) The while statement


2) The do statement
3) The for statement

The while statement


The simplest of all the looping structures in c is the while statement.

Syntax :-
initialization;
while (test condition)
{
body of the loop;
increment/decrement;
}

WAP to print 1…10 using while loop


#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i=1;
while(i<=10)
{
printf(“%d”,i);
i++
}
getch();
}

The for statement


The for loop is an entry-controlled loop that provides a more concise loop control
structure.

Syntax :-
for (initialization;test-condition;incr/decr)
{
body of the loop;
}

#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
for(i=1;i<=10;i++)
{
printf(“%d”,i);
}
getch();
}

The do statement

initialization;
do
{
body of the loop;
incr/decr;
}while(condition);

eg,
void main()
{
int i=1;
do
{
printf(“\n%d”,i);
i++;
}while(i<=10);

The for loop can be specified by different ways

1) for(;;) -> infinite loop -> No arguments


2) for(a=0;a<=20;) -> infinite loop -> ‘a’ is neither increased nor decreased
3) for(a=0;a<=10;a++) ->Display 1 to 10 -> ‘a’ is increased from 0 to 10.
Printf(“%d”,a);
4) for(a=10;a>=0;a--) -> Displays value -> ‘a’ is decreased from 10 to 0
Printf(“%d”,a); from 10 to 0

Note : The initialization section has two parts p=1 and n=1 separated by a comma.
Ex- for(n=1,m=50;n<=m;n=n+1,m=m-1)
{
p=m/n;
printf(“%d%d%d\n”,n,m,p);
}
The multiple arguments in the increment section are separated by commas.

Note : It is also the test-condition may have any compound relation and the testing need
not be limited only to the loop control variables.

e.g, sum=0;
for(i=1;i<20 && sum<100; ++i)
{
sum=sum+i;
printf(“%d%d%d\n”,i,sum);
}

Note : Another unique aspect of for loop is that one or more sections can be omitted, if
necessary.
---------
m=5;
for(;m!=100;)
{
printf(“%d\n”,m);
m=m+5;
}
-------

Note :- If the test-condition is not present, the for statement setup an infinite loop.

Note : We can setup time delay loops using the null statement.
for(j=1000;j>0;j=j-1)
; ->Error
WAP to enter no. and calculate the factorial using for loop.
#include<stdio.h>
#include<conio.h>
void main()
{
int num,i,fact=1;
clrscr();
printf(“\nenter the number :”);
scanf(“%d”,&num);
for(i=1;i<=num;i++)
{
fact=fact*i;
}
printf(“\nfactorial=%d”,fact);
getch();
}
WAP to print this series using for loop.
1
12
123
1234
12345

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf(“%d”,j);
}
printf(“\n”);
getch();
}
WAP to print this series using for loop.

1
22
333
4444
55555
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf(“%d”,i);
}
printf(“\n”);
getch();
}

WAP to print this series using for loop.


12345
1234
123
12
1

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++)
{
printf(“%d”,j);
}
printf(“\n”);
getch();
}
WAP to print this series using for loop.

1
12
123
1234
12345

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,l=1;
clrscr();
for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++)
{
printf(“ ”);
}
for(k=1;k<=l;k++)
{
printf(“%d”,k);
}
l=l+1;
printf(“\n”);
}
getch();
}
WAP to print this pyramid using for loop.

*
***
*****
*******
*********

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,l=1;
clrscr();
for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++)
{
printf(“ ”);
}
for(k=1;k<=l;k++)
{
printf(“*”);
}
l=l+2;
printf(“\n”);
}
getch();
}

WAP to enter number and check no. is prime or not.

#include<stdio.h>
#include<conio.h>
void main()
{
int num,i;
clrscr();
printf(“\nenter the number :”);
scanf(“%d”,&num);
for(i=2;i<num;i++)
{
if(num%i==0)
break;
}
if(i==num)
printf(“\n Number is prime”);
else
printf(“\n Number is not prime”);
getch();
}

WAP to enter number and reverse its digit.


#include<stdio.h>
#include<conio.h>
void main()
{
int num,rev;
clrscr();
printf(“\n Enter the number :”);
scanf(“%d”,&num);
while(num>0)
{
rev=num%10;
num=num/10;
printf(“%d”,rev);
}
getch();
}

WAP to enter number and print sum of its digit

#include<stdio.h>
#include<conio.h>
void main()
{
int num,rev,sum=0;
clrscr();
printf(“\nenter the number :”);
scanf(“%d”,&num);
while(num>0)
{
rev=num%10;
sum=sum+rev;
num=num/10;
}
printf(“sum=%d”,sum);
getch();
}

You might also like