Nested conditional

I have a line of code which reads as follows:

out << (j < 7 ? (i < 4 ? char(0xD1) : char(0xCF)) : (i < 4 ? char(0xBB) : char(0xBC)));

I don't think it's necessary to print the surrounding code; it's placed in nested for loops with i and j as the iterating variables.

This code snippet does what it's supposed to do (that 'out' isn't a typo for 'cout', it's a stringstream!), but is there any way to refine it a little, so that, for example, I only have to type each of "j < 7" and "i < 4" once?

Thanks!

- Arandur
1) Given the way the machine code works only one of the i < 4 will ever be executed on a given iteration, so calculating it in a variable would not make any performance gain.

2) The optimizer is really smart, especially with loops. You'll probably get the same code no matter how you do it given the simplicity of the expression, in fact, it may only serve to make it harder for the compiler to optimize if you start trying to hand-optimizing it.
Topic archived. No new replies allowed.