Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It is called the the conditional operator or alternativly the ternary operator as it a ternary operator (an operator which takes 3 operands (arguments)), and as it's usually the only operator, that does this.</p> <p>It is also know as the inline if (iif), the ternary if or the question-mark-operator.</p> <p>It is actualy a rather useful feature, as they are expressions, rather than statements, and can therefore be used, for instance in <code>constexpr</code> functions, assigments and such.</p> <p>The C++ Syntax is;</p> <pre><code>logical-or-expression ? expression : assignment-expression </code></pre> <p>It's used as;</p> <pre><code>condition ? condition_is_true_expression : condition_is_false_expression </code></pre> <p>That is, if <code>condition</code> evaluates to <code>true</code>, the expression evaluates to <code>condition_is_true_expression</code> otherwise the expression evaluates to <code>condition_is_false_expression</code>. </p> <p>So in your case, <code>result</code> would always be assigned the value <code>1</code>.</p> <p>Note 1; A common mistake that one makes while working with the conditional operator, is to forget that it has a fairly low <a href="http://en.cppreference.com/w/cpp/language/operator_precedence" rel="noreferrer">operator precedence</a>. </p> <p>Note 2; Some functional languages doesn't provide this operator, as they have expression <code>'if...else'</code> constructs, such as OCaml;</p> <pre><code>let value = if b then 1 else 2 </code></pre> <p>Note 3; A funny use case, which is perfectly valid is using the conditional operator, to decide, which of two variable to assign a value to.</p> <pre><code>(condition ? x : y) = 1; </code></pre> <p>Notice the parentheses are necessary, as this is really what you get without them;</p> <pre><code>condition ? x : (y = 1); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload