-=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- (c) WidthPadding Industries 1987 0|542|0 -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=-
SoCoder -> Blogs Home -> Blogs


 
Scherererer
Created : 30 November 2007
 

The Comma Operator

A neat operator I never realized

After browsing through the codeproject, I found an interesting post on the comma operator. typically, we use it like this:


But this is a different way of using it. In this scenario, it allows you to do multiple tasks in the same line. So, you can do this:


this executes expression1, and then sets variable to expression2. It has an interesting application in for loops, however. I made this Fibonacci sequence very short using this method (calculates 10 numbers):

(written in C#, though you can do this in C, C++, etc if you change the console output method)

as you can see, t=b, b=a+b, and a=t are all evaluated within the for loop, therefore saving space.

if you want to see a really cool example of this in a method called obfuscation, check out this article.

 

Comments


Saturday, 01 December 2007, 14:02
JL235
Instinct as you can see, t=b, b=a+b, and a=t are all evaluated within the for loop, therefore saving space.

and making it far more confusing to read. Saving space is pointless if it makes your work more difficult in the long run.

Still I'm sure there are some good examples out there.

|edit| I just read the link, brilliant example! |edit|