4

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)

Rella
  • 65,003
  • 109
  • 363
  • 636
  • One of the things I actually like about Java. – pmr Jun 14 '10 at 18:15
  • Actually, interpreting numbers / pointers as booleans is good for low-level, pointer languages like C and C++. There is no such a thing as a boolean for your processor. (Well, there are the comparison flags.) – isekaijin Jun 14 '10 at 18:17

9 Answers9

7

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.

GManNickG
  • 494,350
  • 52
  • 494
  • 543
5

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 */ {
    // ...
}
isekaijin
  • 19,076
  • 18
  • 85
  • 153
4

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

fredoverflow
  • 256,549
  • 94
  • 388
  • 662
3

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.

Cogwheel
  • 22,781
  • 4
  • 49
  • 67
2

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
Jacob Relkin
  • 161,348
  • 33
  • 346
  • 320
2

In C/C++ you can do

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

In Java you cannot as i has to be a boolean

mmmmmm
  • 32,227
  • 27
  • 88
  • 117
2

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

JAB
  • 20,783
  • 6
  • 71
  • 80
1

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) {

}
meriton
  • 68,356
  • 14
  • 108
  • 175
1

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

Mike Baranczak
  • 8,291
  • 8
  • 47
  • 71