#1

I seem to have somehow luckboxed my way to the review part of this lesson and I honestly don't understand the effective use of if or else or elseif or unless. I don't know how these functions differ exactly and when each one should be correctly applied.

``Replace this line with your code.``

#2

Here is my code:

``````a=20
b=22
if a<b
print "a is less than b!"
elsif b<a
print "b is less than a"
else
print "b is equal to a"
end``````

The lesson was correct but only because I copied their example I don't understand because we first said a is less than b then used elseif to say the opposite then else to say that they are equal. I guess I am confused because there is no absolute definition what role does if else and elseif play in defining these comparisons?? Could we say the same thing only swaping else where elseif is? Or else where if is?? Does my question make sense??

#3

`if-elseif-else` are the building blocks of any modern program. They help us take decisions.

Consider a real-world decision. You are a student, and you only need to carry your Maths book if your maths teacher is coming to give a lecture. You make a mental note like this:

``````if maths_teacher == true
print "bring maths book to school"
else
print "no need to carry maths book!"
end``````

but, you need to carry your geometry book in case the geometry teacher comes in place of maths teacher, so you write:

Elsif refers to that branch of code that is checked only if the previous condition failed.

``````if maths_teacher== true
print "bring maths book to school"
elsif geometry_teacher == true
print "bring geometry book to school"
else
print "no need to carry maths/geomtery book!"
end``````

remember that here the elsif branch is only checked if the `if` condition failed. When you are printing `"bring geometry book to school"` you are certain that the boolean `geometry_teacher` was `true` and `maths_teacher` was `false`. Because `maths_teacher` boolean was `false`, the `elsif` condition was checked and because `geometry_teacher` boolean was `true`, the `elsif` part's `print` statement got executed.

Hope that helps!

#4

Hey @ihabacode,
well, your question is a bit weird.

EDIT: Oh well, @gaurangtandon was a little bit faster than me
Nevermind, here is my solution:

Here a little explanations - in case you don't know how `if-else`-constructs work:

``````/* Here you got two so-called "hard-coded" variables.
Variables can be - as their name says - variable,
so their value can change */
a = 20
b = 22

/* Here starts your if-else construct */
if a < b

/* if the value of a is smaller than the value of b,
this part of the if-elsif-else-construct gets executed
- like in this case */

print "a is less than b!"

elsif b<a

/* If the value of b is smaller than the value of a,
this part of the if-elsif-else-construct gets executed,
- that's not the case */

print "b is less than a!"

else

/* Otherwise, if none of the both upper cases apply,
this part of the if-elsif-else-construct gets executes.
- in this case, this would happen, if both variables would
have the same value */

print "b is equal to a!"

/* And here is the very end of your if-elsif-else */
end``````