Hello and welcome to this video.
In this video I'm going to introduce you to another type of conditional statement caught else statements.
What are else statements l statements are conditional statements that only execute certain block of
code when a condition is false.
So it is different from the if statement and the ill if statement those condition checks for conditions
that are true.
While the L statements will check for conditions that are not true and then execute the block of code
that it has.
So this is what a basic syntax looks like.
For an else statement.
So you start off with the if statement which is the first conditional statement to check if a condition
is true.
You then go into the L if conditional statement which checks if another condition is true.
Now when the condition in the F and the L if are not true It then goes to the else block and execute
the code in the X block.
So remember with the F and the L L if this block of code will only activate or run if the conditions
are true.
Now if those conditions are not true it then jumps to the code inside this ls block and execute the
code.
If the code in the if and the else fails I'm going to illustrate how the else statement works and when
to create some variables so create a variable a and I'll give you the value of 7 or create another variable
B and I'll give it a value of 8 or create or more call it C and give it a value of 9.
So before you can implement an LS block he must have an F block in place.
So I'll start with an F so I'll see if the variable a is greater than the variable B.
Now add my colon and press enter and that gives me the indentation.
So now are right the body of the code that will execute if the condition is true.
So I'm going to reference the variable a symbol and say a comma and then I'll add a string and the string
is basically going to say if a is true that condition the Turks will say a a smaller than our the comma
to escape thus shrink I'll say B.
So now this condition here it's my first condition.
Don't forget the condition in the F block will always test for a true condition.
Next I'm going to add an L if block some say l if if the variable B is greater than or equal s to the
variable c.
Add my call on our tab the tab gives me indentation when you anytime you have a call on the.
In Python that you are writing a new line of code to be indented against the block of code so you know
that each block of code belongs to a particular condition.
So now I'm going to add a print statement so say print and I'm just going to add something that will
print I'll say B which is the value of the variable B and inside that I will add a string.
O say B is smaller than and then escape that string to a comma and add the variable C so this condition
in my if.
BLOCK And in my else if block both have to evaluate to true for the code to execute.
So this code here on line 6 is the F block code that will run if the condition set in line 5 is true
the code block on Line 9 is what will execute if the conditions set online.
It is true.
So if those both conditions are not true this is where the else statement comes in.
Then we want the code inside the else block to execute.
So you are the colon and I just tap and I just do a simple print statement inside that print statement.
I would say C which is a variable C and I'll add a text that will say is larger than I'm going to escape
that string with a comma larger and b not another comma add another string and do a comma to escape
that string and then I'll add an A.
So that is my code completes when the code executes it will check for all this condition this first
condition here is going to evaluate to be false because a which is seven is not greater than B which
is eight.
So this block will not execute so this block here is also false.
B is not greater than or equal to C because B is 8 and c is 9.
So this block of code again will not run cause the condition is false.
We then jump onto this block of code which is the else block this block of code will execute.
There is no condition set up just said if these two conditions are not met I just want this code to
run the code on line 12 is what will execute.
So let me play that so that you can see the result.
So if I press the play button you can see here is printing out this code on line 12 which says 9 is
larger than eight and seven.
This C is the 9 the b is 8 and the A is the 7 so you can see only that code executed because the other
conditions have not been met.
So you only have one block of code execute anyone that matches the condition that block executes.
If for example this condition was true the code will stop there.
The other to this to block will not run at all if this block was true it will stop here and this block
here will not run.
So the else statement is an alternative statement that will run when the conditions you're testing for
is false.
So that's it for this video.
Thank you for watching.
Bye for now.