-
Nested Loops Help.
I was just learning about nested loops and got very confused about the example in Jumping in C++. Can you guys please help me through it and explain what each part of it does. Thanks! This is the code below:
Code:
#include <iostream>
#include <string>
usingnamespacestd;
int main()
{
for(int i=0;i<10;i++)
{
cout<<'\t'<<i;
}
cout<<"\n";
for(int i=0;i<10;++i)
{
cout<<i;
for(int j=0;j<10;++j)
{
cout<<'\t'<<i*j;
}
cout<<"\n";
}
}
-
You should indent the code properly, e.g.,
Code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
for (int i = 0; i < 10; i++)
{
cout << '\t' << i;
}
cout << "\n";
for (int i = 0; i < 10; ++i)
{
cout << i;
for (int j = 0; j < 10; ++j)
{
cout << '\t' << i * j;
}
cout << "\n";
}
}
As for what it does, start with the first loop, the one that isn't nested: what does it do?
-
I don't know. Can you please explain what each part does.
-
I suggest that you read and work through an introductory book on programming in C++. Accelerated C++: Practical Programming by Example by Koenig and Moo, although dated, remains a good book for this purpose.
EDIT:
Oh wait, you are working through an introductory book in C++. So, ignore my suggestion. Rather, go back to the part of the book that explains for loops and work through that part. Go back to the part of the book that explains printing to standard output with cout and operator<< and work through that part. It looks like you sped through to reach the part about nested loops before you have the fundamentals down.