Commas in for loop

H

hprYeV

I have done a reasonable amount of programming in C++,
but the other day I was talking to someone after a
lecture in a course on Java who said that they had not been
used to the syntax of the Java for loop because they always
had been programming in C++. I asked them what it was
they had not been used to, and they said that in C++ you
can use commas to separate the initial statement, the
condition, and the loop statement like this:

for(i=0,i<10,i++)
{ cout<<"hello"<<i<<endl; }

I then thought that maybe what they were thinking of
was this kind of statement

for(i=0,j=5;i<20;i++,j++)
{ cout<<i<<" "<<j<<endl; }

But he reiterated that it was the previous form, that
you can separate the condition and the initial and loop
statements with just a comma. I have never seen this.
I'd tried implementing this in the GNU implementation of
C++, and as I suspected, it returned an error.

I then concluded that he had been using an implementation
of C++ that allows this (which?). Interestingly, he said
he never used the semicolon (;) for a for loop in C++ once.

Thanks for any insight.
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

I have done a reasonable amount of programming in C++,
but the other day I was talking to someone after a
lecture in a course on Java who said that they had not been
used to the syntax of the Java for loop because they always
had been programming in C++. I asked them what it was
they had not been used to, and they said that in C++ you
can use commas to separate the initial statement, the
condition, and the loop statement like this:

for(i=0,i<10,i++)
{ cout<<"hello"<<i<<endl; }

I then thought that maybe what they were thinking of
was this kind of statement

for(i=0,j=5;i<20;i++,j++)
{ cout<<i<<" "<<j<<endl; }

But he reiterated that it was the previous form, that
you can separate the condition and the initial and loop
statements with just a comma. I have never seen this.
I'd tried implementing this in the GNU implementation of
C++, and as I suspected, it returned an error.

I then concluded that he had been using an implementation
of C++ that allows this (which?). Interestingly, he said
he never used the semicolon (;) for a for loop in C++ once.

I think he just remembered wrong, you can have commas but they mean
something different. Consider this code for example:

for (int i = 0, k = 10; i < k; ++i)
// Do something

Here I use commas to declare more than one variable. You can also use
commas in the last statement (++i) if you need to increment more than
one variable for example. You can in fact use a comma in any of the
statements but I just can't figure out any uses for a comma in the
comparison. The important thing however is that a comma can not
separate the different parts in the for-loop since it has a meaning of
its own.
 
H

hprYeV

I think he just remembered wrong, you can have commas but they mean
something different. Consider this code for example:

for (int i = 0, k = 10; i < k; ++i)
// Do something

Here I use commas to declare more than one variable. You can also use
commas in the last statement (++i) if you need to increment more than
one variable for example. You can in fact use a comma in any of the
statements but I just can't figure out any uses for a comma in the
comparison. The important thing however is that a comma can not
separate the different parts in the for-loop since it has a meaning of
its own.

Yeah, just as I thought. I'll have to ask him which compiler
he used.

Thanks
 
A

Alf P. Steinbach

* hprYeV:
I have done a reasonable amount of programming in C++,
but the other day I was talking to someone after a
lecture in a course on Java who said that they had not been
used to the syntax of the Java for loop because they always
had been programming in C++. I asked them what it was
they had not been used to, and they said that in C++ you
can use commas to separate the initial statement, the
condition, and the loop statement like this:

for(i=0,i<10,i++)
{ cout<<"hello"<<i<<endl; }

I then thought that maybe what they were thinking of
was this kind of statement

for(i=0,j=5;i<20;i++,j++)
{ cout<<i<<" "<<j<<endl; }

But he reiterated that it was the previous form, that
you can separate the condition and the initial and loop
statements with just a comma. I have never seen this.
I'd tried implementing this in the GNU implementation of
C++, and as I suspected, it returned an error.

I then concluded that he had been using an implementation
of C++ that allows this (which?). Interestingly, he said
he never used the semicolon (;) for a for loop in C++ once.

Thanks for any insight.

You seem to be confused about "they" and "he".

"They" and "he" seem to be confused about C++ for loops.

Mutual confusion.

But perhaps it was about using a comma-expression as the loop body:

for( int i = 0; i < 10; ++i ) say( i ), say( i );

Which is valid C++ (if there is function 'say'), but not valid Java.
 
H

hprYeV

* hprYeV:

You seem to be confused about "they" and "he".

"They" and "he" seem to be confused about C++ for loops.

Mutual confusion.

But perhaps it was about using a comma-expression as the loop body:

for( int i = 0; i < 10; ++i ) say( i ), say( i );

Which is valid C++ (if there is function 'say'), but not valid Java.

That's neat, I'll ask if that's what _he_ meant. That way
next time I can indicate here what compiler it is that let's
you use commas instead of semicolons.

I'm not confused about they and he, I just use "they" to
mean "that person." Of course, that does not have to do with
the topic at hand.
 
D

David Harmon

On Tue, 20 Mar 2007 06:55:07 GMT in comp.lang.c++, hprYeV
I have done a reasonable amount of programming in C++,
but the other day I was talking to someone after a
lecture in a course on Java who said that they had not been
used to the syntax of the Java for loop because they always
had been programming in C++. I asked them what it was
they had not been used to, and they said that in C++ you
can use commas to separate the initial statement, the
condition, and the loop statement

There is the proof: Java causes brain damage.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top