+ 1

Logical operator help!!!

What is the output of this code? a = 5 If !(a>2) Print (“2”) Elsif !(1+2==a) Print (“3”) Else Print (“4”) End Why is the answer 3? Could some explain step by step? Please and thank you!!!

10th Apr 2019, 6:31 PM
Nirvashtypezero
Nirvashtypezero - avatar
2 Answers
+ 6
a>2 true because a = 5 and 5>2 But ! converts the true in false. So, first print is skipped. 1+2=3==a is false but ! coverts false in true. So, program print 3
10th Apr 2019, 6:40 PM
Javier Felipe Toribio
Javier Felipe Toribio - avatar
+ 2
'!' is used for "not". If not 'a' greater than 5 is the same as saying If 5 less than 2 which is false. Elseif not 1+2 equal 5 is the same as saying If 3 not equal 5 which is true. Be very careful using 'not' in programming. Whilst it certainly has it's use, I don't think I'd ever use it like the way you've shown.
10th Apr 2019, 6:45 PM
Duncan
Duncan - avatar