"If the gender is male, the answer is him, but if the gender is female, the answer is her". A common situation in programming - [is/are], [him/her], [yes/no], [child/children], [ice/water/steam], [public/private] - and C and Perl and PHP provide the "ternary" operator ? and : to provide a shorthand alternative to a code-heavy if/else structure:

$thing = ($stock == 1) ? "item" : "items";

(That's PHP and Perl to set the "thing" variable to item (singular) or items (plural) depending on the value of $stock being equal (or not) to 1.

In Perl, there's always half a dozen ways to do anything, and the same effect can be created using the lazy and and or keywords ... the logic goes something like this:

i) "If both a and b need to be true and we have already discovered that a is false, we don't need to bother to do test b. And indeed to do test b would be inefficient in such a circumstance. Since (a) is a false value, we'll just return that

ii) "If either a or b needs to be true, and we have already discovered that a is true we don't need to bother to do test b. And indeed to do test b would be inefficient in such a circumstance. Since (a) is a true value, we'll just return that

iii) If we have to move on to test the second item in any circumstance, we may as well just return the value of that item as it's going to be a true value if the whole result is true, and a false value if the whole result is false.

I say "almost" the same, because in Lua, the number 0 is a true value, as is an empty string. Only the boolean false, and nil, are really false values. And that's ideal in most circumstances, where you want to be able to accept 0 as just another number - indeed, in Perl 6 you'll be able to say that a number is "0 but true" which rather acknowledges the need for a revision of the "0 is false" mantra. (written 2009-08-12)

Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles

P204 - Perl - Conditionals and Loops[4323] Learning to program - Loop statements such as while - (2014-11-22)[4322] Learning to Program - the conditional statement (if) - (2014-11-21)[4031] Showing what programming errors look like - web site pitfall - (2013-03-06)[3895] Flowchart to program - learning to program with Well House - (2012-10-14)[3397] Does a for loop evaluate its end condition once, or on every iteration? - (2011-08-18)[3004] Increment operators for counting - Perl, PHP, C and others - (2010-10-18)[2832] Are you learning Perl? Some more examples for you! - (2010-06-27)[2711] For loop - checked once, or evety time? Ruby v Perl comparison and contrast - (2010-04-07)[2550] Do not copy and paste code - there are much better ways - (2009-12-26)[1727] Equality and looks like tests - Perl - (2008-07-29)[1696] Saying NOT in Perl, PHP, Python, Lua ... - (2008-07-04)[1607] Learning to program in Perl - (2008-04-11)[1477] Decisions - small ones, or big ones? - (2007-12-18)[1468] Lexical v Arithemetic testing, Bash and Perl - (2007-12-11)[1191] Smart English Output - via PHP and Perl ? : operator - (2007-05-18)[930] -> , >= and => in Perl - (2006-11-18)[353] Wimbledon Neck - (2005-06-20)

U103 - Lua - Conditionals and loops[4574] repeat until in Lua - a one or more rather than a zero or more loop - (2015-11-05)[4272] Lazy operators in Lua - what they mean, and examples - (2014-05-05)[3686] The goto statement in Lua - (2012-04-06)[3558] Python or Lua - which should I use / learn? - (2011-12-21)[2455] Lua examples - coroutines, error handling, objects, etc - (2009-10-15)[2454] For loops in Lua - (2009-10-14)[1738] Clean code, jump free (Example in Lua) - (2008-08-06)[1685] Short circuit evaluation (lazy operator) in Lua - (2008-06-22)

This is a page archived from The Horse's Mouth at
http://www.wellho.net/horse/ -
the diary and writings of Graham Ellis.
Every attempt was made to provide current information at the time the
page was written, but things do move forward in our business - new software
releases, price changes, new techniques. Please check back via
our main site for current courses,
prices, versions, etc - any mention of a price in "The Horse's Mouth"
cannot be taken as an offer to supply at that price.