Counter control structure
Counter control structure is defined as a control structure in which control is executed in a loop or we can say that sequentially upto certain condition is true.
There are three loops in C:-
- While loop
- do while loop
- For loop
While loop
syntex:-
initialization;
while(condition)
{
statement;
increment or decrement condition;
}
Ex.
i=1;
while(number<=5)
{
i++;
printf("%d\n",i);
}
When number 6 is entered by user then loop does not execute because condition is false as number>5.
In this control structure condition is test first then loop execute up to that condition is true, as the condition false control turn out of the loop.
Do while loop
In this type of control structure loop is increment or decrement first then condition checked.In false condition also loop execute atleast once.
suntex;-
initialization;
do
{
statement;
increment or decrement;
}
while(condition);
Ex:-
i=1;
do
{
i++;
printf("%d",i);
}
while(number<=5);
When number 6 is entered then condion is false because number >5 but loop execute once in false condition is also.
which show output as:-
6
For loop
in for loop initialization,increment or decrement,and condition all are written in single line. syntex:-
for(initialize;condition;inc/dec;)
{
statement;
}
Here inc=incerement and dec=decrement.
Ex.
for(i=1;i<=number;i++)
{
printf("%d/n",i);
}
When number 3 is entered by user then it will show output as:-
1
2
3
Friends,
This is the concept of loops.Loops are used in programming language.I hope you will get something from it.
No comments:
Post a Comment