Reputation: 33
I came across a piece of code that is:
for(i=((90.0E1,0x5A)<=(0x158,140.70E1)?(.28,3.45E2,0):(95.30E1,26.40E1)<=1.400E2?(1,this):(108.,0x227));i<length;i++) {
// some other code here
}
Can someone help me by explaining the stuff in the for() brackets?
Upvotes: 3
Views: 334
Reputation: 21199
Simplifying the hex and E numbers, it becomes:
for(i=((900,90)<=(344,1407)?(.28,345,0):(953,264)<=140?(1,this):(108.,551));i<length;i++)
((900,90)<=(344,1407)?(.28,345,0):(953,264)<=140?(1,this):(108.,551)) == 0;
which makes the code equivalent to:
for(i=0;i<length;i++)
It's a very creative and confusing way to make a for
loop, and a good joke.
Upvotes: 0
Reputation: 9060
It is a standard three-expression for
statement, where the first expression, the initializer, happens to be defined as
i = ((90.0E1,0x5A)<=(0x158,140.70E1)?(.28,3.45E2,0):(95.30E1,26.40E1)<=1.400E2?(1,this):(108.,0x227))
In this expression, the ternary ?:
operator, and, to complicate things, does this in a nested fashion.
The syntax of the ?:
operator is the following
condition ? value if true : value if false
Given this, the expression is composed of the following
condition: (90.0E1,0x5A)<=(0x158,140.70E1)
value if true: (.28,3.45E2,0)
value if false: (95.30E1,26.40E1)<=1.400E2?(1,this):(108.,0x227)
The value-if-false holds a nested expression using the ?:
operator which can of course be deconstructed in the same way.
Upvotes: 2
Reputation: 7772
The result of the comma operator is always the value to the right hand side. So each pair of the form (a,b) evaluates to b. Since in your code "a" never has a side effect, we can simply omit it to get:
for(i=(0x5A <= 140.70E1 ? 0 : ...);i<length;i++) {
Where "..." stands for stuff that doesn't matter:
Since 0x5A <= 140.70E1 evaluates to true
, the result of the ?: operator is the value to the right of the question mark, i.e. 0.
So the result is equivalent to
for (i=0; i<length; i++) {
which makes sense.
Upvotes: 5