开发者

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

开发者 https://www.devze.com 2023-02-13 10:58 出处:网络
Is the expressions !(a ==b)a!=b equivalent? i have yes here !a && bb &&!a yes !a || bb ||!a

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


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消