In: Computer Science
x is 5, what is the result of the following Boolean expressions:
1. x != 0
2. x > 0
3. x != 0
4. x > 0
5. (x >= 0) || (x < 0)
6. (x != 1) == !(x == 1)
7. (true) && (3 > 4)
True or False?
Please explain how you got your answers. I've been struggling with Boolean expressions so I'd like a little bit more of an explanation as to why each expression yields a certain answer.
1. x!=0
It will give true. ! mean not. so expresssion mean x is not equal to 5 which is true as 5 is not equal to 1.
2.x>0
It will give true as > mean greater then so 5 is greater then 0, it will yield true.
3. x!=0
It will give true. ! mean not. so expresssion mean x is not equal to 5 which is true as 5 is not equal to 1.
4.x>0
It will give true as > mean greater then so 5 is greater then 0, it will yield true.
5. (x >= 0) || (x < 0)
It will give true. || mean OR which give result true is either left expression of || is true or right expression of || is true. if any of them is true, final reuslt will be true. if both are flase, then only final result will be false. here left side if x>=0 which is true as 5 is greater then equal to 0. right side is false as 5 is not less then 0. As left is true, so final result will be true.
6. (x != 1) == !(x == 1)
It will give true. As lesft side of == is x!=1 (as 5 is not equal to 1) which is true and rigth side of == is !(x==1) which is also true (x==1 is false but ! convert true into false and false into true so will convert false into true). as true == true, so final result is true.(NOTE == is comparision operator and return true if both side has same value as 'true' in our case)
7. (true) && (3 > 4)
It will give false. && is Logical and operator which will resultin true only if both side(left and right) of && are true other wise give false. so as left side is true and rigth side is false(as 3 is not greater then 4). so final result will be false.