By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,889 Members | 1,373 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,889 IT Pros & Developers. It's quick & easy.

Commas in for loop

P: n/a
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.

Mar 20 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On 20 Mar, 07:55, hprYeV <hpr...@icmail.netwrote:
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.

--
Erik Wikström

Mar 20 '07 #2

P: n/a
On Tue, 20 Mar 2007 00:11:46 -0700, Erik Wikström wrote:
On 20 Mar, 07:55, hprYeV <hpr...@icmail.netwrote:
>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.
Yeah, just as I thought. I'll have to ask him which compiler
he used.

Thanks

Mar 20 '07 #3

P: n/a
* 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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 20 '07 #4

P: n/a
On Tue, 20 Mar 2007 08:36:07 +0100, Alf P. Steinbach wrote:
* 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.
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.

Mar 20 '07 #5

P: n/a
On Tue, 20 Mar 2007 06:55:07 GMT in comp.lang.c++, hprYeV
<hp****@icmail.netwrote,
>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.

Mar 20 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.