[英]What does 'Conditional expressions can be only boolean, not integral.' mean?
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)
我不知道Java,我知道C ++ deffenetly不应该强调它意味着什么..请帮助(在比较C ++和Java项目中找到http://www.javacoffeebreak.com/articles/thinkinginjava/comparingc++andjava.html 7子项目1)
It means you need a boolean for a conditional, a conversion from an integral type won't be implicit. 这意味着您需要一个条件的布尔值,来自整数类型的转换将不是隐式的。 Instead of
if (x)
you'd need if (x != 0)
, etc. 而不是
if (x)
你需要if (x != 0)
等。
The former is an int
which will be implicitly converted to bool
in C++ (via != 0
), but the latter expression yields a boolean directly. 前者是一个
int
,它将在C ++中隐式转换为bool
(通过!= 0
),但后一个表达式直接产生一个布尔值。
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);
From a logical point of view, conditional expressions are inherently boolean (true or false). 从逻辑的角度来看,条件表达式本质上是布尔值(true或false)。 However, some languages like C and C++ allow you to use numerical expressions or even pointers as conditional expressions.
但是,某些语言(如C和C ++)允许您使用数字表达式甚至指针作为条件表达式。 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) {
// ...
}
And it would mean this: 这意味着:
if (numericalExpression != 0) {
// ...
}
This allows for concise code, especially in pointer languages like C and C++, where testing for null pointers is quite common. 这允许简洁的代码,尤其是在诸如C和C ++的指针语言中,其中对空指针的测试非常普遍。 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.
在C#或Java等高级语言中,不允许使用数字表达式作为条件表达式。 If you want to test whether a reference to an object has been initialized, you must write:
如果要测试是否已初始化对象的引用,则必须编写:
if (myObject != null) /* (myObject) alone not allowed */ {
// ...
}
Likewise, if you want to test whether a numeric expression is zero, you must write: 同样,如果要测试数值表达式是否为零,则必须编写:
if (numericalExpression != 0) /* (numericalExpression) alone not allowed */ {
// ...
}
In C++, you can say if (someInt)
which is basically equivalent to if (someInt != 0)
. 在C ++中,你可以说
if (someInt)
基本上等于if (someInt != 0)
。 In Java, only the second form is legal. 在Java中,只有第二种形式是合法的。
Take the statement: 采取声明:
if (a > b) {
// Do stuff
}
The "conditional expression" is a > b
. “条件表达式”是
a > b
。 In C++ you can do things like 在C ++中,你可以做类似的事情
int i = foo();
if (i) {
// do stuff
}
This is because integral (integer values) are treated as false when 0 and true otherwise. 这是因为积分(整数值)在0时被视为假,否则为真。 Languages like Java do not allow you to treat integers as boolean values in this way.
像Java这样的语言不允许您以这种方式将整数视为布尔值。
Integral expression: 积分表达式:
int i = 5;
if(i) {
//...
}
//In C/C++, if(i) gets implicitly converted to if(i != 0), Java doesn't do this for you.
Boolean expression 布尔表达式
int i = 5;
if(i==5) {
//...
}
//This will work in Java
In C/C++ you can do 在C / C ++中你可以做到
int i = 5;
if( i ) { ...}
In Java you cannot as i has to be a boolean 在Java中你不能因为我必须是一个布尔值
这意味着,在Java中,布尔值“true”不能与整数值“1”互换(或者更准确地说,与任何非零整数互换),并且布尔值“false”不能与整数互换值“0”。
An expression is code that computes a value. 表达式是计算值的代码。 In both languages an expression has a static type that described the kind of values this expression yields.
在这两种语言中,表达式都有一个静态类型,用于描述此表达式产生的值的类型。 Integral means the the expression's type is int.
积分意味着表达式的类型是int。
Simply put, the authors emphasize each of the following is legal C++ code, but not legal Java code, because the if
's expression yields an integer: 简单地说,作者强调以下每一个都是合法的C ++代码,但不是合法的Java代码,因为
if
的表达式产生一个整数:
if (32) {
}
if (2 * 17 - 33) {
}
int c;
if (c = 12) {
}
To put it another way: C/C++ don't have a real boolean type, they just use integers. 换句话说:C / C ++没有真正的布尔类型,它们只使用整数。 Java does have them - and furthermore, it's got more strict typing than C/C++.
Java确实拥有它们 - 而且,它的输入比C / C ++更严格。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.