Quote: "I did not know about that "comma" operator you explained. Very neat - I don't understand it yet.. butI will LOL "
Think of it this way. It's a perfectly valid statement in C/C++ (and probably C#) to say
4 * 3;
on a line by itself. The only thing is, it doesn't do anything. It evaluates to 12 (IIRC
) and that's all it does. You may even get a warning from the compiler. In order to be of use it either needs to be evaluated as a controlling value or have a side effect such as assignment. If you string a bunch of side effects together with commas in a situation where evaluation is key and finish it off with a controlling evaluation you get multiple things done in one statement. Not that this is desirable in all cases, but some.
You can do some strange things with
while (blaha, blahb, blahc, i++ < 100);
and the same for a do/while loop. I've often done something along the lines of
char c;
while (c = getch(), c != '\n')
even though
while ((c = getch()) != '\n')
works as well. Mostly I use it for tightening up file input to make sure an action takes place before I test.
Another thing to keep in mind, unless they've improved the standards, is that side effect (assignments, for example) aren't guaranteed to take place at the point they appear in the code. The only requirement has been that it must take place by the end of the statement. So something like (and I'm really pulling this out of nowhere)
int i = 6;
a = i++ * (i -2 );
when the i is used in the (i - 2) expression there's no guarantee that it's value has been incremented to 7 nor can any assumption be made that it hasn't been. But it must happen by the time it reaches the ending semi-colon. The comma operator forces side effects to take place when the statement reaches the comma. That's not particularly useful in the above expression but it helps to understand what gets done when.
Lilith, Night Butterfly
I'm not a programmer but I play one in the office