How to represent boolean expressions using if/else statement(s)? Is this right?

2.3k views Asked by At

Is the expressions

!(a ==b)    a!=b equivalent?

i have yes here

!a && b     b &&!a

yes

!a || b     b ||!a

no

And how to write an if/else statement that stimulates the following expression:

z=(100>y) ? z*2 : z/2;

if (100>y)

z=z*2;

else

z-z/2;

what is z= and y= in the end?

i have z=40 and y=12

How to expand the expression y+=2

y=10, z=20
2

There are 2 answers

1
Bartzilla On BEST ANSWER
public static void main(String args[]){

    int a = 1;
    int b = 2;
    int y = 10;
    int z = 12;     

    System.out.println(!(a ==b));
    System.out.println(a!=b);

    if (100 > y) z = z*2; else z = z/2;
    System.out.println(z); 
    System.out.println(y);

    y = y + 2;

    System.out.println(y);

}

Output:

The value for !(a ==b) is: true

The value for (a!=b)) is:true

24

10

12

Additional: Some times (?:) conditional operator is a bit tricky this means that it takes three operands. Together, the operands and the ?: symbol form a conditional expression. The first operand (to the left of the ?) is a boolean expression (i.e., a condition that evaluates to a boolean valuetrue or false), the second operand (between the ? and :) is the value of the conditional expression if the boolean expression is True and the third operand (to the right of the :) is the value of the conditional expression if the boolean expression evaluates to false. For example, the statement:

System.out.println( studentGrade >= 60 ? "Passed" : "Failed" );
1
user207421 On

Your first question is an instance of de Morgan's Laws which you would do well to look up.