本文介绍了什么是“有条件的前pressions只能是布尔值,不完整的。”意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是有条件的前pressions只能是布尔值,不完整的。意思?我不知道Java和我知道C ++ deffenetly没有足够把understend这意味着什么...帮助(在的在比较C ++和Java 7项子项1)

What does 'Conditional expressions can be only boolean, not integral.' mean? I do not know Java and I know C++ deffenetly not enought to understend what it means.. Please help (found in http://www.javacoffeebreak.com/articles/thinkinginjava/comparingc++andjava.html in Comparing C++ and Java item 7 sub item 1)

推荐答案

有条件前pressions由条件和循环控制结构被用于确定一个程序的控制流。

Conditional expressions are used by the conditional and loop control structures to determine the control flow of a program.

// 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);

从逻辑上看,有条件的前pressions天生布尔(true或false)。然而,像C和C ++语言的一些允许你使用数字EX pressions甚至指针作为条件前pressions。当一个非布尔前pression用作有条件前pression,它们被隐式转换成comparisions零。例如,你可以写:

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:

if (numericalExpression) {
    // ...
}

和那意思是:

if (numericalExpression != 0) {
    // ...
}

这使得简洁的code,尤其是在指针语言,如C和C ++,在测试空指针是相当普遍的。然而,让您的code简洁不一定说得清楚。在高级语言如C#或Java,利用数值前pressions有条件EX pressions是不允许的。如果你想测试是否一个对象的引用已初始化,你必须写:

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:

if (myObject != null) /* (myObject) alone not allowed */ {
    // ...
}

同样的,如果你想测试一个数字EX pression是否为零,你必须写:

Likewise, if you want to test whether a numeric expression is zero, you must write:

if (numericalExpression != 0) /* (numericalExpression) alone not allowed */ {
    // ...
}

这篇关于什么是“有条件的前pressions只能是布尔值,不完整的。”意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 08:58