What is looping statements?

What is looping statements ?

Till now we have discussed simple programs involving decision control statements. It provided the ability to choose between alternatives. But these programs still execute each statement almost once and progress from top to bottom. In order to execute the same set of statements repeatedly, we need to understand the concept of loops.
Now let us consider a simple program is which we compute the reciprocal of a number.

#include<stdio.h>
#include<conio.h>
main()
{
float a,b;
clrscr();
printf("enter a number: ");
scanf("%f",&a);
b=1/a;
printf("reciprocol = %f",b);
getch();
}

The program will find out the reciprocal of a single number when it is executed. If we want to find out the reciprocal of 3 numbers, this should be executed 3 times , which wood be time consuming.


Looping causes a certain set of statements in your program to be executed repeatedly certain number of time. Sometimes the required number of repetitions will not be known in advance. The computation simply continues as the expression evaluates to true. Some times situations may also arise in which a group of consecutive instructions are repeated some specified number of times .
The while loop, do-while loop and for loop are various kinds of loop available in C.

  • The while loop :
   The while statements is the most basic looping statements . It lets you execute a set os statements repeatedly as long as condition evaluates to true. It is mostly used in those cases where the programmer does not know in advance how many times the loop will be executed.
Syntax of the while loop,
while (expression)
{
   statement(s);
}  
   next_statement;
}

Where the expression enclosed in parentheses is the condition that controls the loop and may be relational or logical expression. Statement(s) can be a single statement or a block of statements.

When the compiler encounters a while loop in a program, first the expression is evaluated . If it evaluates to true (i.e. non-zero value ) then the statements contained in the loop are executed.
The execution continues as long as the expression remains true. When the conditions become false (zero), the control transfers out of the loop and passes to the next executable statements that follow the loop. Make sure that expression must eventually become false so that the program can terminate. 
The while loop would be more clear after going through the program.

A program of while loop to print sum of 1 to n consecutive numbers, where n is given?
#include<stdio.h>
#include<conio.h>
main()
{
int i=1, sum=0,n;
printf("\how many numbers? :");
scanf("%d",&n);
while(i<=n)
{
sum=sum+i;
i++;
}
printf("sum of first %d consecutive numbers=%d", n, sum);
getch();
}

OUTPUT:
how many numbers?: 5
sum of first 5 consecutive numbers = 15

  • The do-while loop:
The do-while looping statement is somewhat similar to the while loop statements as the loop continues as long as specified condition is true. Unlike the while loop where the test condition is evaluated at the beginning of loop, the do while loop evaluates the condition after the execution of the statements in the body of the loop. So in case of do while , the body of the loop always executes atleast once even the test condition evaluates to false. If the test condition evaluates to true, the body of the loop is executed again and it keeps on executing until the test condition is false and then control transfers to the next executable statement following the do while loop.
The syntax of do while loop is,

do
{
   statement(S)
}
while (expression);

The do-while statement is an exit controlled looping statement as the test condition represented using an expression is checked at the end of each iteration and therefore the body of the loop is executed unconditionally for the first time. For this reason, do -while is used less frequently than the while statement because in practise it is unusual to encounter a situation where you are sure you want to execute the loop atleast once. thus, do-while statement is used where there is necessity to execute a loop once.

A program to display the message 'welcome' 4 times using do-while ?
#include<stdio.h>
#include<conio.h>
main()
{
int count =1;
clrscr();
do
{
printf("\nWELCOME");
count++;
}
while (count<=4);
getch();
}

OUTPUT:
WELCOME
WELCOME
WELCOME
WELCOME

  • The for loop  
The for loop is another form of looping statement which is the most versatile, convenient and popular of three loop structures available in C. It is used in those situations when the programmer knows in advance the number of times a statement or block of statements will be executed. In this loop, the loop-control elements are gathered in one place, while in other loops they are scattered over the program and difficult to understand. The for loop allows us to specify three things about a loop in a single time.
The syntax of for loop is,
for(initial_exp ;  test_expression;  update_exp)
Here,initial_exp is an expression which is used to initialize the loop control variable(s) that is used to control the loop. It is executed once before executing the body of the loop. The test_expression is any relational or logical expression that specifies condition that need to be checked to control the loop control variable. If it evaluates to true , the body of the loop will be executed . If it evaluates to false , the loop terminates and control of execution is transferred to the statement following the for loop.
The  update_exp is an expression that is executed at the end of each loop iteration. It is usually causes an increment or decrement of loop control variable(s). These three expression in the for loop are separated by semicolon and inclosed in parentheses.

A program of for loop to calculate the sum of the first 10 natural numbers.

#include<stdio.h>
#include<conio.h>
main()
{
int sum=0,i;
clrscr();
for(i=1;i<=10;i++)
{
sum+=i;
}
printf("\nsum of first 10 numbers= %d", sum);
getch();
}


OUTPUT:
sum of first 10 numbers= 55
READ MORE



                             THANKS

No comments

Powered by Blogger.