Jump to content
  • Advertisement


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


Conditional assignment ?:

This topic is 5310 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.


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


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


// 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
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

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!