Search code examples
javaclogical-operatorsboolean-expression

Translating C numerical operators to Java Boolean operators


I have the following C code. I want to write a Java version of the C condition but I don't know what boolean operators the C numerical operators represent in the if statement.

The C code is:

if ( (n >= 1) * (n < 10) + (n == 0) )
{
     printf("A\n");
}
else
{
     printf("B\n");
    }

    return 0;

What boolean operators do * and + translate to?


Solution

  • You can figure this out by looking at their truth tables. Keep in mind that in C, 0 is false, and any other value (including 1) is true.

     *  | 0 | 1      +  | 0 | 1
        |   |           |   |
     ---+---+--      ---+---+--
     0  | 0 | 0      0  | 0 | 1
     ---+---+--      ---+---+--
     1  | 0 | 1      1  | 1 | 2
    

    In case this is a homework question, I will leave the rest to you. If you need more hints, Google for "boolean operation truth table".