Loop Control Statements

Loop Control Statements are used when a section of code may either be executed a fixed number of times,or while  same condition is true.C gives you a choice of three types of loop statements,while,do-while and for.

1) The while loop keeps repeating an action until an associated condition returns false.This is useful where are programmer does not know in  advance how many times the loop will be traversed.

2)The do while loop is similar,but the condition is checked after the loop body is executed.This ensures that the loop body is run at least once.

3)The for loop is frequently used,usually where the loop will be traversed a fixed number of times.


The While Loop 

When in a program a single statement or certain group of statements are to be executed repeatedly depending  upon certain test condition,the while statement is used.

The syntax is as follows:

while (test condition)
{
body_of_the_loop;
}

Here,test condition is an expression that controls how long the loop keeps running.Body of the loop is a statement  or group of statements enclosed in braces and are repeatedly executed till the value of test condition evaluates to true.As soon as the condition evaluates to false,the control jumps to the first statement following the while statement.If condition initially itself is false,the body of the loop will never be executed.While loop is sometimes called as entry-control loop,as it controls the execution of the body of the loop depending upon the value of test condition.


Example 

Write a program to calculate the factorial of a given input natural number.

#include<stdio.h>
#include<math.h>

main()
{
int x;
long int fact=1;
printf("Enter any number to find factorial:\n");
scanf("%d",&x);
while(x>0)
{
fact=fact*x;
x=x-1;
}
 printf("Factorial is %d",fact);
}

OUTPUT

Enter any number to find factorial;
4
Factorial is 24

The do...while Loop 

There is another loop control structure which is very similar to the while statement called as the do....while statement.The only difference is that the expression which determines whether to carry on looping is evaluated at the end of  each loop.The syntax is as follows:

do{
statements(s);
}while(test condition);

In do-while loop,the body of loop is executed at least once before the condition is evaluated.Then the loop repeats body as long as condition is true.However,in while loop,the statement doesn't execute the body of the loop even once,if condition is false.That is why do-while loop is also called exit-control loop.


Example 

Write a program to print first ten  even natural numbers.

#include<stdio.h>
main()
{
int i=0;
int j=2;
do{
print("%d",j);
j=j+2;
i=i+1;
}
while(i<0);
}

OUTPUT
2 4 6 8 10 12 14 16 18 20


The for Loop

for statement makes it more convenient to count iterations of a loop and works well where the number of iterations of the loop is known before the loop is entered .The syntax is as follows:

for(initialization;test condition;increment or decrement)
{

statement(s);
}
The main purpose is to repeat statement while condition remains true,like the while loop.But in addition,for provides places to specify an initialization instruction and an increment or decrement of the control variable instruction.So this loop is specially designed to perform a repetitive action with a counter .



Example 

Write a program to print first n natural numbers.


#include<stdio.h>
main()
{
int i,n;
printf("Enter the value of n \n");
scanf("%d",&n);
printf("\n The first %d natural numbers are : \n",n);
for(i=1;i<=n;i++)
{
printf("%d",i);
}
}


OUTPUT

Enter value of n
6
The first 6 natural numbers are :
1 2 3 4 5 6

0 comments:

Post a Comment