deadbite
deadbite

Reputation: 23

Simplify Boolean statements (Java)

I am trying to simplify the following boolean expressions. However, I can't figure them out. I would like to know how to get to the answer. Where b is a boolean andnis anint`

A. if (n==0) {b=true;} else {b=false;} // is it  b=!n; ????
B. if (n==0) {b=false;} else {b=true;}
C. b = false; if (n>1) {if (n<2) {b=true;}}
D. if (n<1) {b=true;} else {b=n>2;}

I also tried to simplify the expressions; is this correct?

b==true      // b
b==false     // !b
b!=true      // !b
b!=false     // b

Any hint or help is appreciated.

Upvotes: 2

Views: 3129

Answers (2)

Emil Laine
Emil Laine

Reputation: 42838

A. b = n == 0;

B. b = n != 0;

C. b = n > 1 && n < 2;

D. b = n < 1 || n > 2;

Upvotes: 1

Elliott Frisch
Elliott Frisch

Reputation: 201447

A.

if (n==0) {b=true;} else {b=false;}
b = (n == 0);

B.

if (n==0) {b=false;} else {b=true;}
b = (n != 0);  

C.

b = false; if (n>1) {if (n<2) {b=true;}}
b = false; // No int is > 1 and < 2.

D.

if (n<1) {b=true;} else {b=n>2;}
b = n < 1 || n > 2;

Upvotes: 3

Related Questions