+ 3
How is the result of this code is 3?
a=5 if !( a > 2) puts ("2") elsif !( 1 + 2 == a ) puts ("3") else puts("4") end
6 Answers
+ 4
look at ! operator before expression. It negates the result of if statement so:
a = 5
!(1+2 == a)
result is "false"
but we've ! before so !false = true that's why this if statement is executed.
+ 2
Thank you all for the answers.
+ 1
The ! means not...Since a= 5, only if 5 is not greater than 2 will the puts 2 be executed. The puts 3 is executed because 1+ 2 is not equal to a 5.
+ 1
as if!(a>2 ) return false
bt elsif !(1+2==a) return true becoz( 1+2==a) return true and the symbol of exclamatory complement tha result of false into true and hence elsif code will execute and control exit the if else block
+ 1
the first expression is true but is negate so you must go to the next expression that is false but is negate and becomes true.. so you find the answer in this statement.
- 2
there is a error in the program .
elsif leads to error.
a=5;
if!(a>2)
puts("2")
else
if!(1+2==a)
puts("3")
else
puts("4")
end
the output is surely 3