+ 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!!!
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
+ 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.