ClanKiller.com
http://forums.clankiller.com/

ternary operation
http://forums.clankiller.com/viewtopic.php?f=24&t=2198
Page 1 of 1

Author:  Satis [ Thu Feb 08, 2007 10:33 am ]
Post subject:  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

Author:  Pig [ Thu Feb 08, 2007 1:57 pm ]
Post subject: 

I love ternaries. Once you get used to them, you use them constantly. It's so much easier and significantly less code.

Author:  RB [ Thu Feb 08, 2007 5:13 pm ]
Post subject:  Re: ternary operation

I'm little off the context...

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

Author:  Satis [ Fri Feb 09, 2007 7:36 am ]
Post subject: 

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]

Author:  Pig [ Fri Feb 09, 2007 12:00 pm ]
Post subject: 

I use it a lot for overiding defaults.
[php]
$limit = ($_GET['limit']) ? $_GET['limit'] : PAGINATION_LIMIT;
[/php]

Author:  RB [ Fri Feb 09, 2007 12:31 pm ]
Post subject: 

(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.

Author:  Satis [ Sat Feb 10, 2007 10:47 am ]
Post subject: 

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.

Author:  RB [ Sat Feb 10, 2007 11:16 am ]
Post subject: 

Offtopic: why certain number of you keeps addressing me as "sir"?
Should I change my nick?

Author:  Satis [ Sun Feb 11, 2007 1:39 am ]
Post subject: 

nah, just a form of speech. Changing your nick won't matter.

A gfree by any other name is just as odd.

Author:  RB [ Sun Feb 11, 2007 5:36 am ]
Post subject: 

However, I dislike that 'sir'. Sounds kinda - distancing?

Page 1 of 1 All times are UTC - 6 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/