0
Can anyone explain what is the meaning of "When an elsif block executes the entire if expression is exited."
About elsif-statement
2 Respostas
+ 1
the if condition can contain as many elseif according to the different conditions possibility , and they all represent as one block ,if the condition matches any elseif statement it will exits the whole if block
0
Imagine a program determines your grades from your score.
score = 75
if score > 80
puts 'A'
elsif score >= 70
puts 'B'
elsif score >= 50
puts 'C'
elsif score >= 40
puts 'D'
else
puts 'F'
end
Now, If we run the program we get 'B' as answer. How so?
our Score was 75, so it also matches the conditions
score >= 50
score >= 40
so, shouldn't we get 'B', 'C', 'D' as answers?
It seems, the code only checked the first 'elsif score >= 70' condition only, and skip all the elsif and else conditions.
That's because, it did just that. If any of the conditions are met, the if-else block stops entirely. It will not check rest of the conditions.
You can tinker the example here
https://code.sololearn.com/cDgvP3223993