Conditional expressions are used by conditional and contour control structures to define the program control flow.
// 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);
From a logical point of view, conditional expressions are inherently logical (true or false). However, some languages, such as C and C ++, allow you to use numeric expressions or even pointers as conditional expressions. When a non-Boolean expression is used as a conditional expression, they are implicitly converted to comparisons with zero. For example, you can write:
if (numericalExpression) {
And that will mean the following:
if (numericalExpression != 0) {
This allows you to use compressed code, especially in pointer languages ββsuch as C and C ++, where null pointer testing is quite common. However, making your code concise does not necessarily make it more clear. In high-level languages ββsuch as C # or Java, using numeric expressions as conditional expressions is not allowed. If you want to check if the object reference was initialized, you should write:
if (myObject != null) {
Similarly, if you want to check if a numeric expression is zero, you should write:
if (numericalExpression != 0) {
source share