in this lesson we'll learn an elegant way of adding a second if statement to one of
our expressions this is done with the help of the Elif kit as shown in this example if Y is
not greater than five the computer will think else if Y is less than five written Elif y is
less than five then I will print out less and the else statement follows as a tail
with a respective block that says return equal let's confirm we wrote the code correctly we
can print out the compared to five function with a value of y equal to ten in the following way
then we'll expect to see a statement that says greater because 10 is greater than 5
correct okay perfect what if we carry out this operation for the number two
the Machine tells us that two is less than five and that's what we expected to obtain the third
outcome we must compare the number five with a number that is not greater or smaller than
five this will happen only if the argument of the function is five right shall we try
this one great we obtained equal as expected know that you can add as many Elif statements
as you need let's provide an example if y is less than zero the string negative should be displayed
I will place the block between the if and the other Elif statement
you
let's see what happens the function with an argument of minus three shows negative just
as it should let me just control whether our little program will run properly if I asked it
to compare to five a value that lies in the range between zero and five say three yes we see less so
everything is okay a very important detail you should try to remember is the computer always
reads your commands from top to bottom regardless of the speed at which it works it executes only
one command at a time scientifically speaking the instructions we give to the machine are part of
a control flow this is something like the flow of the logical thought of the computer the way
the computer thinks step-by-step executing the steps in a rigid order when it works
with a conditional statement the computer's task will be to execute a specific command
once a certain condition has been satisfied it will read your commands from the if statement
at the top through the Elif statements in the middle to the else statement at the end
the first moment the machine finds a satisfied condition it will print
the respective output and will execute no other part of the code from this conditional
in our example if the first statement is correct we will see the corresponding output number one
which is printing the string greater the computer will disregard the Elif in the
else statements and will proceed with the rest of the code if the first statement is not correct we
will move forward and the computer will check whether our second statement is true if yes we
will see output number two which is printing the string negative if not we will get to statement
number three and so on until the computer finds a satisfactory outcome to print out now I will
switch the order of the two Elif statements to prove that the order of instructions matters okay
let me print compared to five of - three
ha instead of negative we obtained less this is how the computer reasons assume y equals minus
3 print out greater if y is greater than 5 is it greater than 5 no so the computer continues
and checks if there are any other statements in our code given we have other statements it
moves forward so is y less than 5 yes it is at this moment the computer thinks lovely I
got it my number is less than 5 i satisfy what my programmer asked me to do I print
out less and I am fine and the machine stops there and does not execute a single letter
of the code that follows in this block the fact that you examine the cases when y is
less than 0 or equal precisely to 5 have no application they become useless whether you
ask for the output of minus 3 or 3 you will still have to be satisfied with the less label
you found this interesting didn't you stay focused for the next lecture
when we will share something more about computational logic thank you for watching