Friday, May 10, 2019

C language : Iterative statement

                      Iterative statement in C language          

  Iterative statement:-  Iterative statements are used to repeat the execution of a list of statement, depending on the value of an integer expression.C language support three types of iterative statement. It is also known as the looping statement. They are-

1. While loop
2 .Do while loop
3. For loop

1 .while loop- 
 The while loop provides a mechanism to repeat one or more statement while a particular condition is true. The syntax of while loop are given below:

statement X;
while(condition)
{
statement block;
}
statement Y;

In the while loop, the condition is tested before any of the statement in the statement block is executed. If the condition is true, only then the statement will be executed otherwise if the condition is false, the control will jump to statement Y, which is the immediate statement  outside the while loop block.

For eg.            /*print first ten numbers */

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

2. do-while loop-
The do-while loop is similar to the while loop. The only difference is that in a do-while loop, the test condition is tested at the end of the loop. Now that the test condition is tested at the end, this clearly means that the body of the loop gets executed at least one time (even if the condition is false ). The syntax of a do-while loop is shown in below: 

statement X;
do
{
statement block;
}
while (condition);
statement Y;

The main disadvantage of using a do-while loop is that it always execute at least once, even if the user enter some invalid data, the loop will execute.
However, the do-while loop are widely used to print a list of option for a menu driven program.

For eg.            /* program for sum and average of first n natural numbers. */

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i=0,sum=0;
float avg=0.0;
printf("Enter the value of n ");
scanf("%d",&n);
do
{
sum=sum+i;
i=i+1;
}
while(i<=n);
avg=sum/n;
printf("\n The sum of first n natural numbers is %d ",sum);
printf("\n The average of first n natural numbers is %f ",avg);
getch();
}

3. for loop-

Like the while and do-while, the for loop provides a mechanism to repeat a task until a particular condition is true. "for loop " is usually known as a determinate or definite loop because the programmer knows exactly how many times the loop will repeat. The number of times the loop has to be executed can be determined mathematically by checking the logic of the loop. The syntax of for loop is given below:

for(initialization ; condition; increment/decrement update )
{
statement block;
}
statement Y;

When the for loop is used, the loop variable is initialized only once. With every iteration of these loops, the value of loop variable is updated and the condition is checked. If the condition is true, the statement block of the loop is executed else the statement comprising the statement block of the loop are skipped and the control jumps to the immediate statement following the for loop body.
            The for loop is widely used to execute a single or a group of statement a limited number of times. Another point to consider is that in a for loop, condition is tested before the statement contained in the body are executed. So if the condition does not hold true, then the body of the loop may never get executed.

For eg.              /* program to print the following pattern */

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,count=0;
for(i=1;i<=5;i++)
{
printf("\n ");
for(j=1;j<=i;j++)
printf("\n %d",count++);
}
getch();
}

2 comments:

Function

FUNCTION C enables programmer to break up a program into segments commonly known as functions, each of which can be written more or...