Code:
for(initialisation;condition;increment/decrement)
{
}
* Initialisation Expression:-
It is executed only when the loop 1'st start. It provides loop variable an initial value.
* Test Expression:-
It involves relation operator. It is executed every time through the loop before the body of the loop is executed . If the test expression is true the loop wil go on otherwise the loop wil end.
* Increment/Decrement (re-initialising) expression:-
It os always executed at the end of the loop after the body of the loop.
Here are some e.g. To help u
* For the following output:
*
* *
* * *
* * * *
* * * * *
Code:
void main()
{
clrscr();
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
cout<<"*"<<" ";
cout<<"\n";
}
}
getch();
}
* For finding out the factorial of any number.
Code:
void main()
{
clrscr();
int i,f,n;
cout<<"Enter the number\n";,
cin>>n;
f=1;
for(i=n;i>=1;i++)
{
f=f*i;
}
cout<<"The factorial is =\t"<< f;
getch();
}
* For displaying the table of any number..
Code:
void main()
{
clrscr();
int i,n,t;
cout<<"Enter the number\n";
cin>>n;
for(i=1;i<=10;i++)
{
t=i*n;
cout<< t;
cout<<"\n";
}
getch();
}
No comments:
Post a Comment