Home > Computer Technology, Problem Solving, Unix-Linux-Os > Ternary Operators – Three is magic

Ternary Operators – Three is magic

November 20th, 2009

Binary’s Sibling Ternary

The ternary operator for many programming languages allows you to use a shorthand when making simple evaluations.  The usual sequence of conditions (and this can vary between languages, so search for ‘your_language ternary operator‘ for more information.)  Using this operator allows us to take ~6 lines of code and replace it with one line of code. In general this is best used selectively and for simple conditions, otherwise use the extra lines of code (regardless of language) so who-ever-has-to-support-your-code will remain sane:)

if  boolean_condition_matches
then
 do_first_thing
else
 do_something_different
end

Note that the code structure requirements will vary between languages so you may need brackets, braces, etc. for your target language.  BTW – most *NIX shells do not directly support the ternary operator…

Simple examples [Boolean | result_if_true | result_if_false]

  • economy = price_of_gas > $2 ?  “Bad” : “Hmmm”  ### set the value of the variable ‘economy‘ to ‘Bad’ or ‘Hmmm’
  • max_lines > 20? max_lines = 0 : max_lines++ ### increment or reset the variable ‘max_lines

Ruby:

  • boolean?   ? do_first_thing    : do_something_different
  • lets_party = (year > 2012)  ? “Party-TIME!”  : “Are_You_Ready?”

Ruby IRB Session example:

irb(main):005:0> year=2010
=> 2010
irb(main):006:0> lets_party = (year > 2012)  ?  "Party-TIME!"  : "Are_You_Ready?"
=> "Are_You_Ready?"
irb(main):007:0> year=2013
=> 2013
irb(main):008:0> lets_party = (year > 2012)  ?  "Party-TIME!"  : "Are_You_Ready?"
=> "Party-TIME!"

Javascript

<script language=javascript>
var b=5;

(b == 5) ? a="true" : a="false"; ### Ternary operator

document.write("Result:  "+a);
</script>
Result: true ### the output in this example

PHP (from php.net)

<?php
// on first glance, the following appears to output 'true'
echo (true?'true':false?'t':'f');

// however, the actual output of the above is 't'
// this is because ternary expressions are evaluated from left to right

// the following is a more obvious version of the same code as above

echo ((true ? 'true' : 'false') ? 't' : 'f');

// here, you can see that the first expression is evaluated to 'true', which
// in turn evaluates to (bool)true, thus returning the true branch of the
// second ternary expression.
?>

Share and Enjoy:
  • LinkedIn
  • Digg
  • del.icio.us
  • Google Bookmarks
  • Technorati
  • Live
  • Slashdot
  • Sphinn
  • Mixx
  • Yahoo! Buzz
  • StumbleUpon
  • Facebook
  • MSN Reporter
  • Reddit
  • RSS
  • Add to favorites
  • FriendFeed

Related posts:

  1. Zena – Rails CMS & VDI Playground CMS=Content Management System Rails = Ruby on Rails – a...
  2. Visits from Murphy – Seagate FreeAgent disk crash… I previously posted about our friend Murphy - he has...
  3. From Bash to Ruby – part 1 Bash - Changing from a proven, ubiquitous tool to a...
  4. Got (global) Stats? Worth a look – note that the tool at gapminder.org...
  5. Google & China – some other thoughts This is not a technical video – it does, however,...

Comments are closed.
________________________________________________
YOUR GeoIP Data | Ip: 38.107.179.222
Continent: NA | Country Code: US | Country Name: United States
Region: CA | State/Region Name: California | City: Glendora
(US only) Area Code: 626 | Postal code/Zip:
Latitude: 34.132099 | Longitude: -117.851097
Note - if using a mobile device your physical location may NOT be accurate...
________________________________________________

Georgia-USA.Com - Web Hosting for Business
____________________________________