unclear "comand"

Hi, can someone tell me what "for(;j;j--)" exactly do in this given "program"?
i used it in class but it's still unclear to me :/


#include<iostream>
using namespace std;

void main()
{
int i,j,n;
cout<<"insert a number ";
cin>>n;
n=n+1;

for(i=1;i<=n;i++)
{
j=i-1;
for(;j;j--)
cout<<"0";
j=i;
cout<<endl;
}
for(i=2;i<=n;i++)
{
j=n-i;
for(;j;j--)
cout<<"0";
j=n-i+1;
cout<<endl;
}

system("Pause");

}
http://www.cplusplus.com/doc/tutorial/control/
the for command takes 3 parameters, separated by semicolons. the first one is the initialization step, the second is the stopping condition, the third is the iteration advancement step. In your case there is no initialization. It relies on the fact that j was already initialized (say the line j=i-1;). The stopping condition tells the for loop to stop when the condition is false. A false is represented sometimes by 0. So when j=0 the loop stops. j-- means that at every iteration the value of j is decreased by 1
thank you. i was confused because the "initialization step"was missing and because i was expecting "j" to grow not to decrease in the first loop but I guess I don't understand the given 'program' as much as I thought..
Too late. Validation for spam behavior. http://www.cplusplus.com/forum/beginner/118509/
Topic archived. No new replies allowed.