“条件表达式只能是布尔值,不能是整数”是什么意思?意思是?

发布于 2024-09-05 05:47:30 字数 302 浏览 11 评论 0原文

“条件表达式只能是布尔值,不能是整数。”是什么意思?意思是?我不知道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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

淡笑忘祈一世凡恋 2024-09-12 05:47:30

这意味着您需要一个布尔值作为条件,从整型类型的转换不会是隐式的。而不是 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 need if (x != 0), etc.

The former is an int which will be implicitly converted to bool in C++ (via != 0), but the latter expression yields a boolean directly.

仅一夜美梦 2024-09-12 05:47:30

条件表达式和循环控制结构使用条件表达式来确定程序的控制流。

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

从逻辑的角度来看,条件表达式本质上是布尔值(真或假)。但是,某些语言(例如 C 和 C++)允许您使用数值表达式甚至指针作为条件表达式。当非布尔表达式用作条件表达式时,它们会隐式转换为与零的比较。例如,您可以这样写:

if (numericalExpression) {
    // ...
}

这意味着:

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

这允许简洁的代码,特别是在 C 和 C++ 等指针语言中,在这些语言中,测试空指针是很常见的。然而,使代码简洁并不一定会使其更清晰。在 C# 或 Java 等高级语言中,不允许使用数值表达式作为条件表达式。如果要测试对象的引用是否已初始化,则必须编写:

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

同样,如果要测试数值表达式是否为零,则必须编写:

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

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). 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) {
    // ...
}

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. 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 */ {
    // ...
}

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

if (numericalExpression != 0) /* (numericalExpression) alone not allowed */ {
    // ...
}
我一向站在原地 2024-09-12 05:47:30

在 C++ 中,您可以说 if (someInt),它基本上等同于 if (someInt != 0)。在Java中,只有第二种形式是合法的。

In C++, you can say if (someInt) which is basically equivalent to if (someInt != 0). In Java, only the second form is legal.

魄砕の薆 2024-09-12 05:47:30

采取语句:

if (a > b) {
    // Do stuff
}

“条件表达式”是 a > b。在 C++ 中,您可以执行类似的操作

int i = foo();
if (i) {
    // do stuff
}

,因为积分(整数值)在 0 时被视为 false,否则被视为 true。像 Java 这样的语言不允许您以这种方式将整数视为布尔值。

Take the statement:

if (a > b) {
    // Do stuff
}

The "conditional expression" is a > b. In C++ you can do things like

int i = foo();
if (i) {
    // do stuff
}

This 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.

妄想挽回 2024-09-12 05:47:30

积分表达式:

int i = 5;
if(i) {
  //...
}

//In C/C++, if(i) gets implicitly converted to if(i != 0), Java doesn't do this for you.

布尔表达式

int i = 5;
if(i==5) {
  //...
}

//This will work in 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
酒中人 2024-09-12 05:47:30

在 C/C++ 中你可以做

int i = 5;
if( i ) { ...}

在 Java 中你不能做,因为 i 必须是一个布尔值

In C/C++ you can do

int i = 5;
if( i ) { ...}

In Java you cannot as i has to be a boolean

累赘 2024-09-12 05:47:30

这意味着,在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".

岛歌少女 2024-09-12 05:47:30

表达式是计算值的代码。在这两种语言中,表达式都有一个静态类型,用于描述该表达式生成的值的类型。整型表示表达式的类型为 int。

简而言之,作者强调以下每一个都是合法的 C++ 代码,但不是合法的 Java 代码,因为 if 的表达式产生一个整数:

if (32) {

}

if (2 * 17 - 33) {

}

int c;
if (c = 12) {

}

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:

if (32) {

}

if (2 * 17 - 33) {

}

int c;
if (c = 12) {

}
旧城空念 2024-09-12 05:47:30

换句话说: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++.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文