+ 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

30th Nov 2016, 12:01 PM
Sampreet Gaonkar
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.
30th Nov 2016, 12:21 PM
Jakub Stasiak
Jakub Stasiak - avatar
+ 2
Thank you all for the answers.
2nd Dec 2016, 2:42 AM
Sampreet Gaonkar
+ 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.
30th Nov 2016, 12:45 PM
Jose Ayala
Jose Ayala - avatar
+ 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
2nd Dec 2016, 1:06 PM
abhishek sharma
abhishek sharma - avatar
+ 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.
3rd Dec 2016, 7:59 PM
Michael Isac Girardi
Michael Isac Girardi - avatar
- 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
30th Nov 2016, 2:21 PM
Mani Maran
Mani Maran - avatar