ternary expression code example

Example 1: if statement shorthand c

if (true)
	printf("This is the shorthand");

// OR

(true) ? (/*run if true*/) : (/*run if false*/);

Example 2: ternary operator

(condition) ? (if true, do this) : (otherwise, do this)

Example 3: conditional operator

var age = 26;
var beverage = (age >= 21) ? "Beer" : "Juice";
console.log(beverage); // "Beer"

Example 4: javascript if shorthand

condition ? doThisIfTrue : doThisIfFalse

1 > 2 ? console.log(true) : console.log(false)
// returns false

Example 5: single if statement js true false

condition ? exprIfTrue : exprIfFalse

Example 6: Ternary Operator

condition ? expression-if-true : expression-if-false;

function findGreater(a, b) {
  return a > b ? "a is greater" : "b is greater";
}

Tags:

C Example