473,320 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Commas in for loop

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
5 3960
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
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
* 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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: ineedyourluvin1 | last post by:
Hello all! I've been looking for a way to strip characters from strings such as a comma. This would be great for using a comma as a delimiter. I show you what I have right now. ...
7
by: AES | last post by:
Encountered a URL containing a comma the other day -- the first time I've ever noticed that, so far as I can recall. It worked fine, however, and I gather commas are legal in URLs. Out of...
6
by: dixie | last post by:
I have a text field on a form which has names with a comma between them like this: 'J. Smith, A. Jones, A. Man, J. Johns'. I am trying to find a procedure that will count the number of people in...
4
by: Alan Lane | last post by:
Hello world: I have a ListBox that I fill as a ValueList from a SQL query. The SQL looks like: SELECT Format(COST,"$#,000") As ItemCost FROM tblPricing ... I put semicolons between each...
27
by: Peter Ammon | last post by:
My code obfuscator gave me this: char buff; to which gcc retorted: "ISO C90 forbids variable-size array 'buff'" and checking the standard, it appears that commas are indeed forbidden...
3
by: Robert Scheer | last post by:
Hi. I have a regularexpression validator control on a page. This regular expression validates a textbox to accept only numbers and commas: validationexpression="*" I am trying to modify this...
4
by: striker | last post by:
I have a comma delimited text file that has multiple instances of multiple commas. Each file will contain approximatley 300 lines. For example: one, two, three,,,,four,five,,,,six one, two,...
27
by: StevePBurgess | last post by:
With a string of authors such as: Carson, David, Milne, Rebecca, Pakes, Francis J., Shalev, Karen, Shawyer, Andrea I would like to write a function to change every other comma into a semi...
9
by: conspireagainst | last post by:
I'm having quite a time with this particular problem: I have users that enter tag words as form input, let's say for a photo or a topic of discussion. They are allowed to delimit tags with spaces...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.