Sometime we need to do a specific task more than one time.In that case,we don't write that statement again and again.We use loop controlling statement.
In " C " there are three loop controlling statement :
1. for
2.while
3.do.....while
for loop :
To use one or more statement for a selected time in " C " we use " for " loop.
Ex :
I need to print " SHUVO " for 50 times on computer screen.Here,no need to write "printf" for 50 times.We can do it more easily by using " for " loop.
for(i=1; i<50; i++)
printf("SHUVO");
The structure of "for loop" is as follow:
for(initialization; condition; increment/decrements)
| | | |
Variable and Assignment Operator <, >, >=, <=, <<, || ++ - -
Initialization :
The starting value of a loop is declared here.Usually,variables and assignment operators( = ) are used here.
Condition :
Here usually used relational expressions.When loop will end,that depends on this conditional section.If the condition become false in this section the program will also be terminated.
Increment :
Declared value of variables in the initialization section turned incremented or decremented in this section.
Watch The Program :
#include<stdio.h>
#include<conio.h>
main()
{
int i=1;
for(i=1; i<=15; i++)
Printf("The Value is ?%d",i);
getch();
}
OUTPUT:
1,2,3,4,5,6,7,8,9,10,11,12,1315
Program Analysis:
-> for ( i=1; .....; ..... )
This is initialization part in for loop.Here the value of i is declared 1.
-> This is the conditional part.If the condition become satisfied printf() function will execute.So when i=1 and 1 is less than 15,condition is true.
-> for ( .......; .......; i++ )
After executing printf(), i++ will execute.The loop will be working and value of i will be increasing by one every time until the condition become false.
Post a Comment