for loop

Hi,

I have some general questions to for loops. I am reading a code and have encountered for loop initialization of this type:


int k=0;
for(j=0; j<1000; j++,k++)
soemthing comes here

Isn’t this the same as:

int k=0;
for(j=0; j<1000; j++)
{k++
soemthing comes here}

What can be in the initialization in the braket after for (for(here))?
What does it mean when it starts with a semi colon? for(; things come here)

Thanks
Isn’t this the same as:

int k=0;
for(j=0; j<1000; j++)
{k++
soemthing comes here}


No, it's (roughly) the same as:

1
2
3
4
5
6
int k=0;
for(j=0; j<1000; j++)
{
  // something comes here
  k++;
}


What can be in the initialization in the braket after for (for(here))?

I'm not sure I understand the question. In any case, your textbook should tell you what you need to know about the general syntax of a for statement.

What does it mean when it starts with a semi colon? for(; things come here)

It means the first part of the for statement is an empty statement - there's no initialisation step. So:

1
2
3
4
5
j=0;
for(; j<1000; j++)
{
  // something comes here
}


Is equivalent to:

1
2
3
4
for(j=0; j<1000; j++)
{
  // something comes here
}


EDIT: And please use code tags when posting code, to make it more readable:

http://www.cplusplus.com/articles/z13hAqkS/
Last edited on
Thanks for the answer!
I apologies for not using the correct environment for my code. I tried to use the code environment but I got an error! without the code environment I could at least post!

I understand my loops now! Thanks :)
Topic archived. No new replies allowed.