Archived

This topic is now archived and is closed to further replies.

Conditional assignment ?:

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m just wondering if anyone ever uses this conditional assignment. I just learned about it today and want to know if it is ever used in complex programs.

Share this post


Link to post
Share on other sites
It''s a convenient replacement for a single if/else statement.
The real point is that it returns a value, while if/else doesn''t (directly).
I do use it extensively in my programs, I''m sure many do.

-Nik

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Some company uses it.. some don''t... Just remember what it does in case you see it in the code.. Personally I don''t use it.. but that''s just me.. I am pretty sure the equilvanlency if statement is just as good as ?: in terms of performance.

Share this post


Link to post
Share on other sites
the only time I use it is to replace statements like this

if(somecondition)
y=something;
else
y=somethingelse

with something like this

y = somecondition ? something : somethingelse;


you can definately get carried away with the conditional, but in this case, I think it''s justified.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
It''s conditional expression or sometimes the ternary operator, not conditional assignment. You don''t need to assign it''s result.

foo(x < y ? 5 : 0);

I use it when the whole expression will still be sufficiently short.

It can also be used on left side of assignments, like this:

int x=0, y=0;
(true ? x : y) = 4; //set value of x
(false ? x : y) = 5; //set value of y
cout << x << " " << y << endl; //prints 4 5

But I have never seen this form used in real code.

Share this post


Link to post
Share on other sites
You can use it in initializer lists, which is kind of cool. I saw that on here about two weeks ago or so.

edit: man I am tired.

[edited by - antareus on January 4, 2004 3:46:36 PM]

Share this post


Link to post
Share on other sites
bah...

amateurs


// select array where we will perform search

const DXDeviceState *pSearchState =
(type == EDS_Render) ? m_pRenderStates
: (type == EDS_Sampler) ? m_pSamplerStates
: (type == EDS_Texture) ? m_pTextureStates
: 0;
DWORD dwSearch =
(type == EDS_Render) ? m_dwNumRenderStates
: (type == EDS_Sampler) ? m_dwNumSamplerStates
: (type == EDS_Texture) ? m_dwNumTextureStates
: 0;

Share this post


Link to post
Share on other sites
ffx: I guarantee that's slower than the equivalent if-else lattice. I've written similar things before, and although they're ego-building, they're never faster or more readable once they get that complex. Just a note.

Later,
ZE.

//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links


[edited by - zealouselixir on January 4, 2004 3:59:34 PM]

Share this post


Link to post
Share on other sites
Just some my old code. Damn things actually start to slow you down. Never use that kind of ?:

Here is assembly

1915: // select array where we will perform search

1916: const DXDeviceState *pSearchState =
1917: (type == EDS_Render) ? m_pRenderStates
1918: : (type == EDS_Sampler) ? m_pSamplerStates
1919: : (type == EDS_Texture) ? m_pTextureStates
1920: : 0;
10011A6E cmp dword ptr [type],1
10011A72 jne Engine::CDirect3DEngine::CreateDeviceState+0BDh (10011a82)
10011A74 mov ecx,dword ptr [this]
10011A77 mov edx,dword ptr [ecx+2D4h]
10011A7D mov dword ptr [ebp-38h],edx
10011A80 jmp Engine::CDirect3DEngine::CreateDeviceState+0F8h (10011abd)
10011A82 cmp dword ptr [type],2
10011A86 jne Engine::CDirect3DEngine::CreateDeviceState+0D1h (10011a96)
10011A88 mov eax,dword ptr [this]
10011A8B mov ecx,dword ptr [eax+2D8h]
10011A91 mov dword ptr [ebp-3Ch],ecx
10011A94 jmp Engine::CDirect3DEngine::CreateDeviceState+0F2h (10011ab7)
10011A96 cmp dword ptr [type],3
10011A9A jne Engine::CDirect3DEngine::CreateDeviceState+0E5h (10011aaa)
10011A9C mov edx,dword ptr [this]
10011A9F mov eax,dword ptr [edx+2DCh]
10011AA5 mov dword ptr [ebp-40h],eax
10011AA8 jmp Engine::CDirect3DEngine::CreateDeviceState+0ECh (10011ab1)
10011AAA mov dword ptr [ebp-40h],0
10011AB1 mov ecx,dword ptr [ebp-40h]
10011AB4 mov dword ptr [ebp-3Ch],ecx
10011AB7 mov edx,dword ptr [ebp-3Ch]
10011ABA mov dword ptr [ebp-38h],edx
10011ABD mov eax,dword ptr [ebp-38h]
10011AC0 mov dword ptr [pSearchState],eax
1921: DWORD dwSearch =
1922: (type == EDS_Render) ? m_dwNumRenderStates
1923: : (type == EDS_Sampler) ? m_dwNumSamplerStates
1924: : (type == EDS_Texture) ? m_dwNumTextureStates
1925: : 0;
10011AC3 cmp dword ptr [type],1
10011AC7 jne Engine::CDirect3DEngine::CreateDeviceState+112h (10011ad7)
10011AC9 mov ecx,dword ptr [this]
10011ACC mov edx,dword ptr [ecx+2E0h]
10011AD2 mov dword ptr [ebp-44h],edx
10011AD5 jmp Engine::CDirect3DEngine::CreateDeviceState+14Dh (10011b12)
10011AD7 cmp dword ptr [type],2
10011ADB jne Engine::CDirect3DEngine::CreateDeviceState+126h (10011aeb)
10011ADD mov eax,dword ptr [this]
10011AE0 mov ecx,dword ptr [eax+2E4h]
10011AE6 mov dword ptr [ebp-48h],ecx
10011AE9 jmp Engine::CDirect3DEngine::CreateDeviceState+147h (10011b0c)
10011AEB cmp dword ptr [type],3
10011AEF jne Engine::CDirect3DEngine::CreateDeviceState+13Ah (10011aff)
10011AF1 mov edx,dword ptr [this]
10011AF4 mov eax,dword ptr [edx+2E8h]
10011AFA mov dword ptr [ebp-4Ch],eax
10011AFD jmp Engine::CDirect3DEngine::CreateDeviceState+141h (10011b06)
10011AFF mov dword ptr [ebp-4Ch],0
10011B06 mov ecx,dword ptr [ebp-4Ch]
10011B09 mov dword ptr [ebp-48h],ecx
10011B0C mov edx,dword ptr [ebp-48h]
10011B0F mov dword ptr [ebp-44h],edx
10011B12 mov eax,dword ptr [ebp-44h]
10011B15 mov dword ptr [dwSearch],eax


Nasty ha...

Share this post


Link to post
Share on other sites
I'll use it whenever it applies. I used it in one of my programs for a class I'm taking at school where I had to delete a certain number of characters and I wanted to make sure it was positive so it looked like delete(x > 0 ? x : 0) and my teacher had never seen the operator before. Kind of scarry.

Edit: I've never used it like ffx showed, I only have one conditional. In that case I'd just use if statements. So far I've only used it on the right hand of an assignment and when passing an argument to a function.

[edited by - Aerolithe on January 4, 2004 4:34:03 PM]

Share this post


Link to post
Share on other sites
?: is useful in obnoxiously scoped code segments. For example, member initializer lists, initialization of const variables, initialization of references and inside of macros where the block expansion of if/else would bork up expression/statement distinctions.

In reasonably complex code, I''d be somewhat suprised if I didn''t see at least one initializer list with a ?: inside and very suprised if I didn''t see at least one macro used with a ?: inside.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by antareus
You realize thats a lot harder to read, right?
I thought he used it in a way that is very clear to read, clearer, less repetitive and simpler than the alternatives using switch/case or if/elses. Well, not everyone (e.g. you) shares my taste but you gotta see that this isn''t a situation of "realizing" something as you put it.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by Anonymous Poster
clear to read, clearer
Ew, maybe I should be less repetitive myself

Share this post


Link to post
Share on other sites