Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Conditional expressions are used by the conditional and loop control structures to determine the control flow of a program.</p> <pre><code>// conditional control structure if (conditionalExpression) { codeThatRunsIfConditionalExpressionIsTrue(); } else { codeThatRunsIfConditionalExpressionIsFalse(); } // basic loop control structure while (conditionalExpression) { codeThatRunsUntilConditionalExpressionIsFalse(); } // run-at-least-once loop control structure do { codeThatRunsAtLeastOnceUntilConditionalExpressionIsFalse(); } while (conditionalExpression); </code></pre> <p>From a logical point of view, conditional expressions are inherently boolean (true or false). However, some languages like C and C++ allow you to use numerical expressions or even pointers as conditional expressions. When a non-boolean expression is used as a conditional expression, they are implicitly converted into comparisions with zero. For example, you could write:</p> <pre><code>if (numericalExpression) { // ... } </code></pre> <p>And it would mean this:</p> <pre><code>if (numericalExpression != 0) { // ... } </code></pre> <p>This allows for concise code, especially in pointer languages like C and C++, where testing for null pointers is quite common. However, making your code concise doesn't necessarily make it clearer. In high-level languages like C# or Java, using numerical expressions as conditional expressions is not allowed. If you want to test whether a reference to an object has been initialized, you must write:</p> <pre><code>if (myObject != null) /* (myObject) alone not allowed */ { // ... } </code></pre> <p>Likewise, if you want to test whether a numeric expression is zero, you must write:</p> <pre><code>if (numericalExpression != 0) /* (numericalExpression) alone not allowed */ { // ... } </code></pre>
 

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