“条件表达式只能是布尔值,不能是整数”是什么意思?意思是?
“条件表达式只能是布尔值,不能是整数。”是什么意思?意思是?我不知道Java,我知道C++,但不足以理解它的含义。请帮忙(在http://www.javacoffeebreak.com/articles/thinkinginjava/comparingc++andjava.html 比较 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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
这意味着您需要一个布尔值作为条件,从整型类型的转换不会是隐式的。而不是
if (x)
,您需要if (x != 0)
等。前者是一个
int
,它将隐式地在 C++ 中转换为bool
(通过!= 0
),但后一个表达式直接生成布尔值。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 needif (x != 0)
, etc.The former is an
int
which will be implicitly converted tobool
in C++ (via!= 0
), but the latter expression yields a boolean directly.条件表达式和循环控制结构使用条件表达式来确定程序的控制流。
从逻辑的角度来看,条件表达式本质上是布尔值(真或假)。但是,某些语言(例如 C 和 C++)允许您使用数值表达式甚至指针作为条件表达式。当非布尔表达式用作条件表达式时,它们会隐式转换为与零的比较。例如,您可以这样写:
这意味着:
这允许简洁的代码,特别是在 C 和 C++ 等指针语言中,在这些语言中,测试空指针是很常见的。然而,使代码简洁并不一定会使其更清晰。在 C# 或 Java 等高级语言中,不允许使用数值表达式作为条件表达式。如果要测试对象的引用是否已初始化,则必须编写:
同样,如果要测试数值表达式是否为零,则必须编写:
Conditional expressions are used by the conditional and loop control structures to determine the control flow of a program.
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:
And it would mean this:
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:
Likewise, if you want to test whether a numeric expression is zero, you must write:
在 C++ 中,您可以说
if (someInt)
,它基本上等同于if (someInt != 0)
。在Java中,只有第二种形式是合法的。In C++, you can say
if (someInt)
which is basically equivalent toif (someInt != 0)
. In Java, only the second form is legal.采取语句:
“条件表达式”是
a > b。在 C++ 中,您可以执行类似的操作
,因为积分(整数值)在 0 时被视为 false,否则被视为 true。像 Java 这样的语言不允许您以这种方式将整数视为布尔值。
Take the statement:
The "conditional expression" is
a > b
. In C++ you can do things likeThis is because integral (integer values) are treated as false when 0 and true otherwise. Languages like Java do not allow you to treat integers as boolean values in this way.
积分表达式:
布尔表达式
Integral expression:
Boolean expression
在 C/C++ 中你可以做
在 Java 中你不能做,因为 i 必须是一个布尔值
In C/C++ you can do
In Java you cannot as i has to be a boolean
这意味着,在Java中,布尔值“true”不能与整数值“1”(或者更准确地说,与任何非零整数)互换,并且布尔值“false”不能与整数互换值“0”。
It means that, in Java, the boolean value "true" is not interchangeable with the integer value "1" (or, more accurately, with any non-zero integer), and the boolean value "false" is not interchangeable with the integer value "0".
表达式是计算值的代码。在这两种语言中,表达式都有一个静态类型,用于描述该表达式生成的值的类型。整型表示表达式的类型为 int。
简而言之,作者强调以下每一个都是合法的 C++ 代码,但不是合法的 Java 代码,因为
if
的表达式产生一个整数: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.
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/C++ 没有真正的布尔类型,它们只使用整数。 Java 确实有它们 - 而且,它比 C/C++ 有更严格的类型。
To put it another way: C/C++ don't have a real boolean type, they just use integers. Java does have them - and furthermore, it's got more strict typing than C/C++.