Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Loop Statement

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

LOOP CONTROL STRUTURES

Definition:
A loop is defined as a block of statements which are repeatedly executed for certain number of
times.
Terms used in loop:

 loop variable
 initialization
 test condition
 incrimination/decrimination
Loop variable: It is a variable used in the loop.
Initialization: It is the first step in which starting and final values is assigned to the loop
variable. Each time the updated value is checked by the loop itself.
Test condition: It is used to test the condition.
Incrimination/Decrimination: It is the numerical value added to or subtracted from the variable
in each round of the loop.
Two ways of loop control structures:

 Entry controlled loop


 Exit controlled loop

Entry

False
test con

True
Body of the
loop
Three types of loop control structures:

 For loop
 While loop
 Do-while loop

For loop:

 For loop allows to execute a set of instructions repeatedly until the certain becomes
false.
 Assigning variable values, incrementation or decrementation and condition checking is
done in for statement only, where as other control structures are not offered all these
features in one statement.

Syntax:

for ( initialization; test-condition; incrementation/decrementation)


{
body of the loop;
}

Eg:

........

........

int i, sum=0, n ;

for ( i=1; i<=n; i++)

sum = sum + i;

printf (“sum = %d”, sum);

........

........
Program: /* To find factorial for given number */

#include<stdio.h>
#include<conio.h>
main ( )
{
int n, fact=1,i;
clrscr ( );
printf ("enter the number for factorial=");
scanf ("%d", &n);
for (i=1; i<=n; i++)
{
fact = fact*i;
}
printf ("The factorial is=%d", fact);
getch();
}
Output:
Enter the number for factorial=5
The factorial is=120

Nested loops:

 The loop within the loop is called nested loop.


 In nested for loop two or more for statements are included in the body of the loop.
 The number of iterations in this type of structure will be equal to the number of iterations
in the outer loop multiplied by the number of iterations in the inner loop.
Syntax:

for ( initialization; test-condition; incrementation/decrementation)


{
for ( initialization; test-condition; incrementation/decrementation)
{
body of the loop;
}
}

Eg:

........

........

int i, j;

for ( i=1; i<=3; i++)

printf (“/n”);

for ( j=1; j<=3 ; j++)

printf (“sum = %d\t”, j );

........

........

Program: /*To find prime numbers for given number*/

#include<stdio.h>

#include<conio.h>

main()

int n ,t, i, j;
clrscr ( );

printf ("Enter the range");

scanf ("%d", &n);

printf ("Prime numbers are:\n");

for (i=1; i<n; i++)

t=0;

for (j=2; j<i; j++)

if((i % j)==0)

t=1;

break;

if (t==0)

printf ("%d\n", i);

getch ( );

Output:

Enter the range=20

Prime numbers are:

1 2 3 5 7 11 13 17 19
While loop:

The while is an entry-controlled loop statement. The test-condition is evaluated and if the
condition is true, then the body of the loop is executed. After execution of the body, the test-
condition is once again evaluated and if it is true, the body is executed once again. This process
of repeated execution of the body continues until the test-condition finally become false and the
control is transferred out of the loop. On exit, the program continues with the statement
immediately after the body of the loop.

Syntax:

while (test-condition)
{
body of the loop
}
Eg:

........

........

int n, sum=0, i =1;

printf (“Enter n numbers: ”);

scanf (“ %d”, &n );

while ( i <= n)

sum = sum +i;

i++;

printf (“\n Sum = %d\n”, sum );

........

........
Program: /* To find sum of individual digits */

#include<stdio.h>

#include<conio.h>

main()

int s,n=0,r;

clrscr();

printf("Enter a value ");

scanf("%d",&s);

while(s!=0)

r=s%10;

n=n+r;

s=s/10;

printf("Sum of digits = %d",n);

getch();

Output:

Enter a value 456

Sum of digits=15

Do-while loop:

 The do-while, the condition is checked at the end of the loop.


 The do-while loop will execute at least one time even if the condition is false initially.
 The do-while loop executes until the condition becomes false.
Syntax:

do
{
body of the loop
}
while (test-condition);

Eg:

........

........

do

printf (“ \n CSE is the best department “);

i++;

while ( i<=5)

........

........

Program:

#include<stdio.h>

#include<conio.h>

main()

int a=0,b=1,c,s=0;

clrscr ( );

printf ( "Enter a value ");


scanf ("%d",&c);

printf ("%d %d ",a,b);

do

s=a+b;

a=b;

b=s;

printf (" %d ",s);

s=a +b;

while (s<=c);

getch ( );

Output:

Enter a value 23

0 1 1 2 3 5 8 13 21

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

You might also like