Try to imagine you need to write this line "This is all bla bla bla bla bla ",
10 times ,I know that's fun !.What if you need to write it 1000 times ,oh now the boring stuff,
well that is one of the reason of creation for/while/do while loops
so we can write 1000 lines of "This is all bla bla bla bla bla " using for loop just in second or 2 or 3 or... at this way
1 2 3 4 5 6 7 8 9 10
# include <iostream>
usingnamespace std;
int main()
{
for(int i=0;i<1000;i++)
cout << "Line " << i+1 << " : This is all bla bla bla bla bla " << endl;
return 0;
}
We can also draw a Triangle using two for loop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# include <iostream>
usingnamespace std;
int main()
{
// This is a program to draw triangle only with two for loop
for(int i=0;i<7;i++)// I need 7 lines
{
for(int j=0;j<i;j++)// Write "*" j times
cout << "*" ;
cout << endl;
}
}
also fun stuff
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# include <iostream>
# include <ctime>
usingnamespace std;
int main()
{
// This one countdown
double startTime = 0;
for(int i=10;i>=1;i--)
{
startTime = clock();
cout << i << endl;
for(;(clock()-startTime)!=1000;);
}cout << "Fire !" << endl;
return 0;
}
Or to initialise an array like BasicNewbie did
All you need to know is : how many times you want to do something
Line 1 : This is all bla bla bla bla bla
Line 2 : This is all bla bla bla bla bla
Line 3 : This is all bla bla bla bla bla
....
Line 1000 : This is all bla bla bla bla bla
can you put some output instead of you keep asking for "is it count 0-1000"?
can you try it out first?
I feel that a lot thread that you posted , you haven't try it out and keep asking here only.. can do some experiment on it?
Try it bro..
As I also basic here , I also keep learning & try it out..
why don't you just do it ? keep it up.
All you need to know is : how many times you want to do something
Try the exemples above,then try to undestand theme, then we'll show you couple of other uses of for;
for expemle can try to write a program for as that shows only the pair numbers from 0 to 30 ,the only thing you gonna use is for loop ,post your program so we can see It
# include <iostream>
usingnamespace std;
int main()
{
int rows, colms;
char key;
cout << "How many Rows = ";
cin >> rows;
cout << "How many Columns = ";
cin >> colms;
cout << "What is the character to print";
cin >> key;
for (int x = 0; x < rows; x++)
{
for (int y = 0; y < colms; y++)
{
cout << key;
}
cout << "\n";
}
return 0;
}
I need to print Triangles not like your example. its like this,
char a ='*';
for ( int a = 1; a <=6; a++)
{
for (int b = 0; b < 9-a; b++ )
{
cout<<(" ");
}
for (int b = 0; b < 2*a - 1; b++ )
cout<<'*';
cout<<( "\n");
}
already give you the concept , try to do it yourself.
#include<iostream>
#include<conio.h>
usingnamespace std;
void main()
{
char a ='*';
for ( int a = 1; a <=6; a++)
{
for (int b = 0; b < 9-a; b++ )
{
cout<<(" ");
}
for (int b = 0; b < 2*a - 1; b++ )
cout<<'*';
cout<<( "\n");
}
getch();
}
second loop is for the space which is " "
You try to decrease it and you will see what is the function are
for the 3rd loop.
it's to print the "*" .
actually you can delete the char a="*";
my fault. no need to declare that char variable
Thanks. Everything is fine. I'm using DevC++. In my software I tried to prevent closing but I can not. Earlier I used system ("pause"). but someone in this forum told me to not to use it. I checked the forum and read some pages those codes not working in my application. Do you have a different way? Your software perfectly working on Windows 7? What is the version?