C langugae : Loops

While Loop
The while statement, like other such statements, is a looping statement that controls the execution of a series of other statements. Looping statements cause parts of a program to execute repeatedly, as long as a certain condition is being met.
while (test expression) { block of one or more C statements; }
Exp: -
// To print the series 1,2,3,4...............n terms using while loops
#include< stdio.h >
#include< conio.h >
void main( )
{
int r=1,c;
printf(" enter the value \n");
scanf("%d", &c);
while(r<=c)
{
printf("%d\n",r);
r=r+1;
}
getch();
}
The parentheses around test expression are required. As long as the test expression is True (nonzero), the block of one or more C statements executes, repeatedly, until the test expression becomes False (evaluates to zero). Braces are required before and after the body of the while loop, unless you want to execute only one statement. Each statement in the body of the while loop requires a semicolon at the end.
The test expression usually contains relational, and possibly logical, operators. These operators provide the True-False condition checked in the test expression. If the test expression is False when the program reaches the while loop for the first time, the body of the while loop does not execute at all. Regardless of whether the body of the while loop executes no times, one time, or many times, the statements following the while loop's closing brace execute if and when the test expression becomes False
Because the test expression determines when the loop finishes, the body of the while loop must change the variables used in the test expression. Otherwise, the test expression never changes and the while loop repeats forever. This is known as an infinite loop, and you should avoid it.
For Loop
The for loop gathers all three actions (initializing, testing, and updating) into one place. By using a for loop, you can replace the preceding program .Unlike the while and do-while loops, the for loop is a determinate loop.The while and do-while loops continue only until a condition is met. The for loop does this and more: It continues looping until a count (or countdown) is reached. After the final for loop count is reached, execution continues with the next statement, in sequence.
for ( [expression1]; [expression2]; [expression3] )
statement
#include< stdio.h >
#include< conio.h >
// To print table for using for loop
main()
{
int r,c,x;
printf(" enter the value \n");
scanf("%d",&c);
clrscr();
x=c;
for(r=1;r<=10;r++)
{
x=c*r;
printf("%d\n",x);
}
getch();
}
expression1 : Initialization
Evaluated only once, before the first evaluation of the controlling expression, to perform any necessary initialization.
expression2 : Controlling expression
Tested before each iteration. Loop execution ends when this expression evaluates to false.
expression3 : Adjustment
An adjustment, such as the incrementation of a counter, performed after each loop iteration, and before expression2 is tested again.
do...while Statements
The while loop and the for loop are both entry-condition loops. The test condition is checked before each iteration of the loop, so it is possible for the statements in the loop to never execute. C also has an exit-condition loop, in which the condition is checked after each iteration of the loop, guaranteeing that statements are executed at least once. This variety is called a do while loop.
#include< stdio.h >
#include< conio.h >
main( )
{
int r=1,c,x;
printf(" enter the value \n");
scanf("%d",&c);
clrscr();
do
{
x=r*r;
printf("%d\n",x);
r++;
}
while(r<=c);
getch();
}
The loop body statement is executed once before the controlling expression is evaluated for the first time. Unlike the while and for statements, do ... while ensures that at least one iteration of the loop body is performed. If the controlling expression yields true, then another iteration follows. If false, the loop is finished.


Free Web Hosting