Shorthand conditional statements

This article uses ECMAScript as its example language. See bellow for language-specific information. The shorthand conditional statement, or ?:, is also know as the ternary operator.

Although it exists in virtually all programming languages, the conditional operator ?: remains a relatively obscure part of any language. Surprisingly enough though, it remains a very useful operator can save you a bunch of lines when you don’t need the complex stuff.

However, its broad ommitance from books and codes remains justified, ?: is far from being as readeable in comparison to its cousin if. But that’s to take for granted, since it is afterall a shorthand statement.

if (new Date().getHours() < 11)
{
    var timecode:String = "AM";
}
else
{
    var timecode:String = "PM";
}

Traditionally, the example figured just up, using the if conditional statement, would be used. However, such a simple conditional statement shouldn’t necessarily take up 8 lines of code and can instead be stuffed in a magical one line statement when declaring your timecode variable.

var timecode:String = (new Date().getHours() < 11) ? "AM" : "PM";

Although limited to an if else form, the ?: can be  super useful for very quick conditional assignements on a single line. The usage is simple:

boolean-value ? if-true-this-executes : if-false-this-executes ;

So yes, it does the exact same as an if else. If you still have difficulty grapsing on the concept of ?:, the following if else conditional statement has been converted to fit in a ?: conditional statement. Note however that this form is incorrect, it is just for comprehension of the functionality. For additional visual cues, the second line represents the correct form, with color-coded magic so you can clearly understand, and the rest represents a correct if else conditional statement to compare.

(boolean-value) if { when-true } else { when-false };
 boolean-value  ?    when-true   :      when-false  ;

if (boolean-value)
{
    when-true;
}
else
{
    when-false;
}

Here is a list of languages that enable this form, and specifics about them. Despite the if else conditional statement varying significantly amongst languages, the ?: is surprisingly similar.

[ad#google]

Ruby
They call it a short-if statement. Uses the exact same syntax as shown in the colored example but ommits the statement end semicolon entirely.

ECMAScript
Every ECMAScript compliant dialect (Javascript, JScript, ActionScript, etc.) should support the ?; conditional statement with the exact same syntax as shown above. Semicolon is optional.

PHP
Uses the exact same syntax as shown above in the colored example. Semicolon is mandatory.

Java
Uses the exact same syntax as shown above in the colored example. Semicolon is mandatory.

C#
Uses the exact same syntax as shown above in the colored example. Semicolon is mandatory.

C
Uses the exact same syntax as shown above in the colored example. Semicolon is mandatory.

C++
Uses the exact same syntax as shown above in the colored example. Semicolon is mandatory.

Objective-C
Uses the exact same syntax as shown above in the colored example. Semicolon is mandatory.

VB.net
Uses a different syntax:

IIf(condition, when-true, when-false)

Python
Uses a diffrent syntax:

when-true if boolean-value else when-false