Hello and welcome to this video.
In this video I'm going to introduce you to if statements if statements are basically conditional statements
that means they are statements that only execute based on certain conditions.
If statements are important aspects of the control flow of the program they help you make important
decisions in your programs.
They only run the code in the if block when the condition is true.
So when you write an if statement you'll have a block of code that you want to execute.
If the condition that you have set is met so only that particular code will execute if the condition
is not met then nothing runs that code does not execute.
If statements normally can use comparison and logical operators to check if certain conditions have
been met or not this is a basic syntax for writing and if statement so you have the if keyword and then
inside the parentheses you just specify the conditions and then you have this call on here.
This cologne is very important.
It tells the Python interpreter that you are starting a new line of code.
So when you have this call on here it intent if you have a good text editor it will automatically indent
into a new line that enables you to write some new line of code.
So whatever code you're right here in the indented block that is the code that will run or execute if
the condition that you have set in the F block is met to illustrate how the if statement works.
I'm going to create a new file inside my PI charm idc.
So I've already got a directory with some Python files so I'm just going to right click on my directory
and go new and click on Pipeline file.
I'm just going to call it if I need to save it as a python file with a top P Y extensions are quick.
OK so now I've created a new file.
The first thing I want to do is create a couple of variables.
So I'm going to create a variable I'm going to call it a.
I'm going to give it a value of seven.
I'm going to create another variable and I'm going to call these B and I'm going to give it a family
of eight so I want to use the if statement to check for setting conditions before I can run what's inside
this file.
So I'll see if a and then I'm going to use an operator to check I'm going to say if a is less than B
and now add my call on the colon is very important.
When I press Penta you can see is well indented indentation is very important in Python.
So if you're using pi charm it should automatically indented for you.
So now is prompting me to enter a new line of code.
So I'm just going to say print and inside my print parentheses we just see the a variable comma there
now our entire text.
I would say is and then I do a comma to exit that string and then I'll say B B here is referring to
the value of these variable B.
So once I've done I just save my file.
Now what this will do.
Basically I'm using this operator here which is the less than sine to check for condition the condition
I'm checking for is is the value of a variable a is a less than the variable B 7 which is the value
of variable a is less than the value of variable B.
So the condition is true.
And then this block of code will execute.
Now if the condition is false nothing happens.
The code will not run it will just do nothing.
So let me run this code so that you can see all right click and go play.
You can see here is output at 7 which is the value of variable a is smaller than 8.
Now if the condition was false nothing will happen.
Say 5 reverses for example if I changed that to a greater than that will make the condition false because
a which is 7 is not greater than B which is 8.
So now let me try and run this again.
You can see nothing runs you see the code does not execute this block here it does not execute.
OK so what ever code you have inside the F block.
This here.
Well I put the print function is known as the F block so if the condition I set here is wrong or if
it's false this code will not run the code will only run if the condition inside the F block is true
or evaluates to true.
So if statements are very important in your control flow always remember the indentation and the call
on any way you see a colon here.
You telling the interpreter that you want to start a new line of code and make sure it is indented.
If it's not indented the code will not run.
The interpreter will complain are set for this video on if statements.
Thanks for watching and bye for.