Skip to main content
gamedev.net
🔒 Locked

Conditional assignment ?:

Started by sevak Jan 4, 2004 at 1:45 PM 12 replies 950+ views
Original Post
sevak
sevak
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.

www.computertutorials.org
computertutorials.org-all your computer needs...
Nik02
Nik02
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
Niko Suni
Pseudo
Pseudo
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.
antareus
antareus
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]
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
Toni Petrina
Toni Petrina
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;
So... Muira Yoshimoto sliced off his head, walked 8 miles, and defeated a Mongolian horde... by beating them with his head?

Documentation? "We are writing games, we don't have to document anything".
zealouselixir
zealouselixir
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]
[twitter]warrenm[/twitter]
Toni Petrina
Toni Petrina
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...
So... Muira Yoshimoto sliced off his head, walked 8 miles, and defeated a Mongolian horde... by beating them with his head?

Documentation? "We are writing games, we don't have to document anything".
antareus
antareus
quote:
Original post by ffx
bah...

amateurs


You realize thats a lot harder to read, right?

--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
Aerolithe
Aerolithe
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]
SiCrane
SiCrane
?: 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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.