Friday, March 20, 2009

Tutorial: Learn All The Basics Of C++ Language:page 13

I dont know why but this is the only loop which i finds the best. This loop is an entry controlled loop means condition should be fulfiled to enter it.The syntax of this loop is as follows:--

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