Looping Statements in C language.
•Looping Statement:
A computer program is set of statements, Which is normally executed sequentially . But in most of the case it is necessary to repeat certain steps to meet a specific condition . This repetitive operation is done through a loop control structure.
A loop is basically the execution of sequence of statement repeatedly untile a particular condition is true or false . In C language following looping statement are used:
•While Statement:
The while loop statement or a set of Statement until a certain condition is true .
Syntax:
while (condition)
{
statement
}
Here, the condition may be any expression having non -zero value . The loop continues untile the condition is true . When the condition fails , the program body attached with loop (Statement),will not be executed.
Example: to print first 20 natural numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
Working of this statement is similar to the working of while-statement but in this , at least one time the attached loop (statement) is executed ,no matter whether the condition is true or false.
Syntax:
do
{
statements
} while (condition);
Example: The print first 10 odd number.
#include<stdio.h>
void main()
{
int a=1;
do
{
printf ("%d" ,a);
a + = 2;
{ While (a<=19);
}
The for loop is an ideal looping Statement when we know how many times the loop will be executed.
Syntax:
for (initialization; condition; counter)
{
statements
}
Here,
• Initialization is generally an assignment which is used to set the loop control variable, eg ,a=0
• Condition is always tells the limit of the loop or determines when to exit the loop.
eg, a<10
• Counter defines how the loop control variable will change each times the loop is repeated . This may be incremented or decremented.
eg,, a++, a-- .
Example:To print your name 10 times.
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
char name [20];
clrscr();
printf ("Enter your name");
gets(name);
for(i = 1; i<=10; i++)
{
puts( name);
}
getch();
}
Harshit kumar
Admin
Comments
Post a Comment
DON'T COMMENT LINK.