Which means "Conditional expressions can only be logical, not integer." I mean?

Which means "Conditional expressions can only be logical, not integer." I mean? I do not know Java, and I know that C ++ is not enough to understand what this means. Please help (found at http://www.javacoffeebreak.com/articles/thinkinginjava/comparingc++andjava.html comparing C ++ and subtitle of Java 1 article 1)

+4
source share
9 answers

Conditional expressions are used by conditional and contour control structures to define the program control flow.

// 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 logical (true or false). However, some languages, such as C and C ++, allow you to use numeric expressions or even pointers as conditional expressions. When a non-Boolean expression is used as a conditional expression, they are implicitly converted to comparisons with zero. For example, you can write:

 if (numericalExpression) { // ... } 

And that will mean the following:

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

This allows you to use compressed code, especially in pointer languages ​​such as C and C ++, where null pointer testing is quite common. However, making your code concise does not necessarily make it more clear. In high-level languages ​​such as C # or Java, using numeric expressions as conditional expressions is not allowed. If you want to check if the object reference was initialized, you should write:

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

Similarly, if you want to check if a numeric expression is zero, you should write:

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

This means that you need a boolean for a conditional expression, conversion from an integral type will not be implicit. Instead of if (x) you will need if (x != 0) , etc.

The first is an int , which will be implicitly converted to bool in C ++ (via != 0 ), but the last expression leads to Boolean directly.

+7
source

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

+4
source

Take the statement:

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

"Conditional expression" a > b . In C ++, you can do things like

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

This is because integral (integer values) are considered false when 0 and true otherwise. Languages ​​such as Java do not allow integers to be treated as logical values ​​in this way.

+3
source

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 
+2
source

In C / C ++ you can do

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

In Java you cannot, since I have to be logical

+2
source

This means that in Java, the boolean value "true" is not interchangeable with the integer value "1" (or, more precisely, with any non-zero integer), and the boolean value "false" is not interchangeable with the integer value "0".

+2
source

An expression is code that computes a value. In both languages, an expression has a static type that describes the types of values ​​that this expression gives. The integral means that the type of the expression is int.

Simply put, the authors emphasize that each of the following is legitimate C ++ code, but not legal Java code, because the if gives an integer:

 if (32) { } if (2 * 17 - 33) { } int c; if (c = 12) { } 
+1
source

In other words: C / C ++ does not have a real Boolean type, they just use integers. Java has them, and in addition, it has received more stringent typing than C / C ++.

+1
source

Source: https://habr.com/ru/post/1312785/


All Articles