# Off Topic (But Interesting)

This topic is 4909 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I've just been playing around a little with C++ and discovered that you can put an if/else expression within the actual condition of an if statement - to derive a condition. Like so:
int a = 5;
int b = 10;

if ((a<b?a:b) < 7)
{
cout << "yes\n";
}

And then after you conceptualize that - you can move on to some crazy brain twister like so:
int a = 5;
int b = 10;

int c = 3;
int d = 20;

char* word1 = "That's Not So Cool";
char* word2 = "That's Cool";

if (((a<b?a:b) < (d>c?d:c)))
{
cout <<(d<b?word1:word2)<<"\n";
}

... Umm ... I know some people are thinking "SO" But I think it's pretty cool. NOTE: There is no real use for something like this. I just thought I'd share it. [Edited by - Protocol 0 on March 8, 2005 5:52:50 AM]

##### Share on other sites
However, I wouldn't overuse it - it mostly makes for unreadable code. You can even put more ?: conditionals into the if condition, but if a reader needs five minutes to figure out one line of code, that code isn't worth much.

For example, even this slight change:

if(min(a, b) < 7) {  ...}

with min() defined elsewhere is way easier to read, because you moved the additional condition *out of* the if statement.

##### Share on other sites
This is where a coding style change may be necessary. I disagree with the readbaility issue sins the ?: conditional is incredibly simple.

int a = 5;

int b = 10;

if ( ( a < b ? a: b ) < 7 )

{

cout << "yes\n";

}

ace

##### Share on other sites
I'm just wondering. Maybe this could be realy usefuly. I mean, basing an "if" condition; upon another condition. You could than have an "if" statements which could perform dozens of different conditions - based on other conditions.
e.g

if () <- in there you would put as many ?'s as you want - which could completely change the if statment altogether. Saving lines and lines of code. You could save using 20 if statements, by just using one complex, well desinged "if" statments, with if/else expressions within the actual condition.

##### Share on other sites
umm, you know it compiles down to the same kind of thing as just having more if's...

-Greg

##### Share on other sites
Quote:
 Original post by Greg Kumm, you know it compiles down to the same kind of thing as just having more if's...-Greg

No. That's false.

In the case of having them seperately, then yes. But using an if/else expression to define the condition of an "if" statement? Picture that. It's not the same thing. How would the compiler, in this case, compile it as multiple "if" statements if the "if" expression within the statement is actually used to define the "if" statement. It wouldn't work.

##### Share on other sites
Quote:
 Original post by Protocol 0No. That's false.In the case of having them seperately, then yes. But using an if/else expression to define the condition of an "if" statement? Picture that. It's not the same thing. How would the compiler, in this case, compile it as multiple "if" statements if the "if" expression within the statement is actually used to define the "if" statement. It wouldn't work.

Greg is right. At the ASM level they reduce to a chain of if statements. To use your original example, it's roughly like this:

int temp;if(a < b) temp = a;else temp = b;if(temp < 7){ ... }

Of course it looks different on the ASM level and is probably optimized to be a bit more arcane (maybe even eliminates the need for an intermediate variable, possibly just keeping it in a register), but that's the basic idea.

All this "trick" does is make the code more obscure. It's indeed a cool feature of the language, but as you yourself said it has no real use. Compilers are very good at making compact, efficient machine code, so unless you are literally to the point where shuffling instructions is vital to execution speed, always opt for more readable and cleanly laid out code. Besides, if you're at the point where shuffling instructions is vital to execution speed, you don't need to be running through a compiler anyways.

##### Share on other sites
Quote:
 Original post by Protocol 0No. That's false.In the case of having them seperately, then yes. But using an if/else expression to define the condition of an "if" statement? Picture that. It's not the same thing. How would the compiler, in this case, compile it as multiple "if" statements if the "if" expression within the statement is actually used to define the "if" statement. It wouldn't work.

Methinks someone needs to learn to read assembler output.

##### Share on other sites
Quote:
 Original post by Protocol 0No. That's false.In the case of having them seperately, then yes. But using an if/else expression to define the condition of an "if" statement? Picture that. It's not the same thing. How would the compiler, in this case, compile it as multiple "if" statements if the "if" expression within the statement is actually used to define the "if" statement. It wouldn't work.
No. That's false.

##### Share on other sites
Quote:
Original post by Protocol 0
Quote:
 Original post by Greg Kumm, you know it compiles down to the same kind of thing as just having more if's...-Greg

No. That's false.

In the case of having them seperately, then yes. But using an if/else expression to define the condition of an "if" statement? Picture that. It's not the same thing. How would the compiler, in this case, compile it as multiple "if" statements if the "if" expression within the statement is actually used to define the "if" statement. It wouldn't work.
Greg's right. Your code, in x86 assembly, would be something like (a little ugly, it's been a while since I've programmed in assembly):
; ...cmp a, bjl push_ajge push_bpush_a:  push a  jmp endOfPushpush_b:  push bendOfPush:  pop eax  cmp eax, 7  jl printYes  jge endprintYes:  ; Print yes messageend:
And
if (a < b) {  if (a < 7) {    std::cout << "yes\n";  }} else {  if (b < 7) {    std::cout << "yes\n";  }}
would generate the same assembly code.

1. 1
2. 2
3. 3
4. 4
frob
13
5. 5

• 16
• 13
• 20
• 12
• 19
• ### Forum Statistics

• Total Topics
632171
• Total Posts
3004555

×