Forgot username/password? Don't have an account?
Login with Facebook
Home » Courses » Conditionals 2: 'switch' » The Ternary Operation (:?)
Subscribe for updates.
* New to us? log in with Facebook and get a free day pass

Conditionals 2: 'switch'

  1. Overview (01:59)
  2. Switch (06:59)
  3. Breaking The switch Down (11:06)
  4. Switch In Action: Days Of The Month (13:09)
  5. What if Can't Do... (09:17)
  6. The Ternary Operation (:?) (07:39)
  7. The Ternarator (06:53)

The Ternary Operation (:?)

So far, we've seen how, in certain contexts, a switch construct can be used as a convenient shorthand for if-else constructs. we'll now look at a special operator that works with basic types of the language, which takes this short-hand business one step further. This is the ternary operator, ?: . It can be used for two-choice if-else constructs. For example, if we had the following piece of code using an if-else construct, our sequence of writing shorthand code would see the following progression:

var max:int = 0;

var a:int = … ;

var b:int = … ;

// FIRST: coded using the if-else

if(a > b){

max = a;

}else{

max = b;

}

trace(max);

// OR coded using the ternary operator

max = a>b ? a : b;

trace(max);

Note that the variable type of 'max' on the LHS (Left-hand side) must match with the variable types 'a' and 'b' on the RHS (Right-hand side.) The general semantics of the usage of the ternary operator is as follows:

<result> = <IF this condition is true> ? <THEN this-outcome> : <ELSE this-outcome>;

// what follows the ? is the result if the condition is true. If the condition is false, what follows the : is the result. Looks complex: is very simple if you read it like an 'if' statement: IF condition true ? (THIS-outcome) : (ELSE this outcome.)

As a further note, the expression on the RHS always returns a value of some type, so it can be used as an expression in statements, including function calls. So, the code in part 'b' in the example above can be optimized as:

trace( a>b ? a : b);

We have reduced the code clutter by a great deal (actually, we have eliminated the need for declaring and using the variable 'max' completely. If you notice, we were simply outputting the value stored in max to the user interface, not performing any other computation with it.)

So, the bottom line is this: if you are dealing with 2-way decisions, and are merely using built-in types in logical expressions in conditionals, then opt for the ternary operator. It might look a wee bit unwieldy in the beginning, but as you get familiar with it (as any prolific programmer would,) you will understand the importance of brevity in code. If you remember , we had stressed the need and value for readable and well structured code in non-trivial software projects. You now see how well the ternary operator works for both readability and well-structured code.

Got A Question?

'I love being Prime'

Have a question but don't want to ask it publicly? If you are a prime member just ask right here and we will get back to you within 48 hours. Not prime yet? no worries you can ask publicly below.

Act now while we have available seating

Our on line school is private, affordable and interactive with trainer support.
Act now save your seat before someone else takes yours.
Act now