Understanding the comma seperator in loop

If you have something like this,

1
2
3
  do{
    //stuff
  while(a, b, c, d > 100);


So what exactly is happening here. Same for for loops something like,
1
2
3
for(i = 0, j >7, i < 10;i++){

}
All operations in this comma separated list are processed while the result of the last operation is used [by the while/for loop]
The comma operator only returns the value of the last expression so the first loop is equivalent to:
1
2
3
do{
	//stuff
} while(d > 100);


The second loop is not syntactically correct because you have only one semi-colon.
Last edited on
Yes that's what I got from the ref, so what is the point of a, b and c if you say the above, do they not act as conditions?
No, they are pointless.
So why put them there, why have the option?
So why put them there
When I see something like this it's often written by a beginner that hasn't learned to use && yet. In that case I would guess this is what he intended to do:
1
2
3
do{
	//stuff
} while(a  > 100 && b > 100 && c > 100 && d > 100);


why have the option?
You can use any expression you want as a loop condition as long as the return type can be converted to a bool.
I think you are right I think it's a mistake, is this the same for for loops with a comma operator?

If not do you have an example of using commas in a for loop?
1
2
3
for(i = 0, j = 7; i < 10;i++, j++){

}
Thanks. So I take it something like,

1
2
3
for(i = 0, j = 7; i < 10, j < 8;i++, j++){

} 


Doesn't make sense/work?
It is legal C++, and and it will run. However, the only condition which will have any effect on whether it keeps looping will be j < 8. It is NOT the same as writing:

for(i = 0, j = 7; i < 10 && j < 8;i++, j++)
Topic archived. No new replies allowed.