It is currently Thu Mar 28, 2024 4:08 pm



Reply to topic  [ 10 posts ] 
ternary operation 
Author Message
Felix Rex
User avatar

Joined: Fri Mar 28, 2003 6:01 pm
Posts: 16646
Location: On a slope
Reply with quote
Post ternary operation
ok...this is cool but confused me for awhile. I get it now and it's not hard, but I'm going to share the syntax and a quick explanation for anyone that needs it (probably not gfree or pig, but I can use it as a reference and anyone that nubier than me may get something out of it.

Basically a ternary operation is shorthand for an if-then-else statement. php:

[php]
if($var == true){
$var = false;
}
else{
$var = true;
}
[/php]

that's a standard and very simple if/then/else statement in php. Any c-like language has something similar. the ternary equivalent is

[php]
$var = ($var == true) ? false : true;
[/php]

much, much, much easier to write, and once you're used to it, much easier to read a series of conditionals. Anyway, as a quick description

$var_to_set = (conditional statement) ? value_to_set_var_to_if_true : value_to_set_var_to_if_false;

easy enough, right?

here's a wikipedia entry on the subject
http://en.wikipedia.org/wiki/Ternary_operation

_________________
They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety.


Last edited by Satis on Thu Feb 08, 2007 3:21 pm, edited 1 time in total.



Thu Feb 08, 2007 10:33 am
Profile WWW
Duke
User avatar

Joined: Mon Mar 31, 2003 8:59 am
Posts: 1358
Location: right behind you
Reply with quote
Post 
I love ternaries. Once you get used to them, you use them constantly. It's so much easier and significantly less code.


Thu Feb 08, 2007 1:57 pm
Profile YIM WWW
Emperor
User avatar

Joined: Wed Apr 16, 2003 1:25 am
Posts: 2560
Reply with quote
Post Re: ternary operation
I'm little off the context...

sample:
[php]
$var = ($var == true) ? false : true;
[/php]
replacement:
[php]
$var = !$var;
[/php]

_________________
++


Thu Feb 08, 2007 5:13 pm
Profile WWW
Felix Rex
User avatar

Joined: Fri Mar 28, 2003 6:01 pm
Posts: 16646
Location: On a slope
Reply with quote
Post 
heh, indeed. I was only trying to be simple. How about something a little more down to earth?

javascript:

[php]function imageEdit(){
var thespan = document.getElementById("imageUpload");
thespan.style.display = (thespan.style.display != 'none') ? 'none' : 'inline';
}[/php]

_________________
They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety.


Fri Feb 09, 2007 7:36 am
Profile WWW
Duke
User avatar

Joined: Mon Mar 31, 2003 8:59 am
Posts: 1358
Location: right behind you
Reply with quote
Post 
I use it a lot for overiding defaults.
[php]
$limit = ($_GET['limit']) ? $_GET['limit'] : PAGINATION_LIMIT;
[/php]


Fri Feb 09, 2007 12:00 pm
Profile YIM WWW
Emperor
User avatar

Joined: Wed Apr 16, 2003 1:25 am
Posts: 2560
Reply with quote
Post 
(Since Sat posted JS...)
Oh, but they are widely used in OCC too.
Code:
#include <stdio.h>
#define z(x) ma ## in(x)

char*w,t=-2,x=0;z(_){return(++t,_==1)
? w="jgf@aje@aaj@ajj@jig@#",z(0):(w[t]==1<<6
? (putchar(x),x=0,z(0)):w[t]!=043
? (x*=0xA,x+=(w[t]==0152
? 0:(w[t]-'a'+1)),z(0)):0);}

Note the row of "?"-s. They belong to the ternary operators.

_________________
++


Fri Feb 09, 2007 12:31 pm
Profile WWW
Felix Rex
User avatar

Joined: Fri Mar 28, 2003 6:01 pm
Posts: 16646
Location: On a slope
Reply with quote
Post 
that, sir, is gobbledy-gook masquerading as code.

I actually looked over the shoulder of one of our programmers yesterday... he was trying to debug some code. It was all in c++ and it looked something like that, with comments dead center in the code block. As much as I know, sometimes I feel like I know not a goddamned thing.

_________________
They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety.


Sat Feb 10, 2007 10:47 am
Profile WWW
Emperor
User avatar

Joined: Wed Apr 16, 2003 1:25 am
Posts: 2560
Reply with quote
Post 
Offtopic: why certain number of you keeps addressing me as "sir"?
Should I change my nick?

_________________
++


Sat Feb 10, 2007 11:16 am
Profile WWW
Felix Rex
User avatar

Joined: Fri Mar 28, 2003 6:01 pm
Posts: 16646
Location: On a slope
Reply with quote
Post 
nah, just a form of speech. Changing your nick won't matter.

A gfree by any other name is just as odd.

_________________
They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety.


Sun Feb 11, 2007 1:39 am
Profile WWW
Emperor
User avatar

Joined: Wed Apr 16, 2003 1:25 am
Posts: 2560
Reply with quote
Post 
However, I dislike that 'sir'. Sounds kinda - distancing?

_________________
++


Sun Feb 11, 2007 5:36 am
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 10 posts ] 

Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware.