In for, if or other statements that you should use curly parenthesis have a
hidden curly parenthesis if you do not use manually, but the hidden one only
includes one line of code that comes after the for or if, what else it
comes, . So In your code, because of that hidden curly parenthesis the codes
except the first line (cout<<"Hello";) are executed five times.
#include <iostream>
using namespace std;
int main()
{
for (int counter = 0; counter < 5; counter++)
{ for (int inside = 0; inside < 10; inside++)
{//hidden curly parenthesis
cout <<"Hello ";
}//end of hidden curly parenthesis
cout <<"World: ";
}
}
Cheers,
Firat KARAKUSOGLU
_____
From: [email protected] [mailto:[EMAIL PROTECTED] On Behalf Of
len_kim
Sent: 11 Mart 2007 Pazar 03:08
To: [email protected]
Subject: [c-prog] Nested for loops
Hi all,
I'm still new to all this... I am having a bit of difficulty
understanding something
about nested for loops. In the below program I have 2 cout statements
in the inside for loop. The cout <<"Hello "; runs 20 times, as
expected, but why does the cout<<"World "; only run 5 times when it
appears that it is also part of the inside loop. It seems that the
cout<<"World "; is obeying the laws of the outside for loop, not the
inside for loop. I tried adding a 3rd and 4th cout statement in the
inside loop, and they also ran 5 times, so would appear to be part of
the outside loop as well... Can someone shed some light as to why
this
is? I hope my question is clear.
#include <iostream>
using namespace std;
int main()
{
for (int counter = 0; counter < 5; counter++)
{ for (int inside = 0; inside < 10; inside++)
cout <<"Hello ";
cout <<"World: ";
}
}
[Non-text portions of this message have been removed]