Write about various Looping (or) Iterative Statements Available in ‘C’?
https://www.computersprofessor.com/2016/05/write-about-various-looping-or.html?m=0
The statements that are used to
execute the block of the statements repeatedly are called looping statements.
C language provides 3 constructs for
performing loop operations. They are:
1.while statement
2.do statement
3.for loop
1.The while statement:
The simplest of all the looping
structures in C is the while statement .The general form is
Syntax:
while (test condition)
{
body of loop;
}
The while is an entry controlled
loop statement. The test–condition is evaluated & if the condition is
true, the body is executed.
After execution of the body, the
test condition is once again evaluated & if it is true, the body is
executed once again.
This process of repeated execution
of the body continues until the test condition. Finally becomes false &
the control is transferred out of the loop.
The body of the loop may have one or
more statements. The braces are needed only if the body has only contains 2
or more statements.
Ex:–
Write a program to calculate the sum
of squares of all integers between 1 & 10.
#include<stdio.h>
#include<conio.h>
main()
{
int sum=0, n=1;
while (n<=10)
{
sum
= sum+n*n;
}
printf(“sum is %d”, sum);
getch();
}
2. the do statement:–
While statement is used to test the
condition before the loop is executed. The body of the loop may not be
executed at all if the condition fails.
Do statement is used to test the
condition after the loop is executed. The body of the loop executed at least
once eve through the condition is fail.
On some occasions it might be
necessary to execute the body of the loop before the test is performed. In
such care do statement is used. The general form is
Syntax:
do
{
Body of the loop;
}
while (test condition);
In do statement, the program
proceeds to evaluate the body of the loop first at the end of the test–condition.
In the while statement is evaluated. If the condition is true, the program
continues to evaluate the body of the loop once again.
This process continues as long as
the condition is true. When the condition becomes false, the loop will be
terminated & the control goes to the statement that appears immediately
after the while statement.
Since the test condition is
evaluated at eh bottom of the loop, the do–while construct provides a
exit–controlled loop & therefore, the body of the loop is always executed
at least once.
Ex:–
Wap to find sum & avg of n
natural numbers.
#include<stdio.h>
#include<conio.h>
main()
{
int sum, n;
float avg;
printf(“enter n value”);
scanf(“%d”, &n);
i=1, sum=0, avg=0;
do
{
sum=sum+i;
i++;
}
while (i<=n);
avg=sum/n;
printf(“sum=%d\n avg=%f”,sum,avg);
}
3.For Statement:
The for loop is another entry
controlled loop that provides a more concise loop control structure i.e., in
for loop initialization, testing& incrementation of the loop done in the
single statement. The general form is
Syntax:–
for(initialization;test-condition;increment)
{
body of loop ;
}
The execution of the for statement
is as follows.
1. Initialization:
Initialization of
the control variable is done first, using assignment statement such as i = 1
& count = 0. The variable i & count are known as loop–control
variables.
2. Test–condition: The value of the control variable is tested
using the test–condition; is a relational exp, such as i<10 determines="" exit.="" loop="" o:p="" that="" the="" when="" will="">10>
|
If the condition is true, the body
of the loop is executed. Otherwise the loop is terminated & the execution
continues with the statement that immediately follows the loop.
3.
Incrementation/decrementation: When
the body of the loop is executed, the control is transferred back to the for
statement after evaluating the last statement in the loop.
Now, the control variable is
incremented using an assignment statement such as i = i+1 & the new value
of the control variable is again tested to see whether it satisfies the loop
condition.
If the condition is satisfied, the
body of the loop is again executed. This process continues till the value of
the control variable falls to satisfy the test–condition.
Ex:–
Wap to calculate factorial of a
given number
#include<stdio.h>
#include<conio.h>
main()
{
int fact = 1, I, n;
printf(“enter n value”);
scanf(“%d”, &n);
for(i=1;i
{
fact =fact*i;
}
printf(“factorial of a given no
is %d “, fact);
}
comparison
of the 3 loops :