Hello guys and welcome back.
Today we're going to learn about IF statements.
So what is an if statement.
Well see you can do things such as conditions so I can say if a variable A is equal to five then do
this particular action else if it isn't equal to five.
Do something else.
So I can explain all of that today.
SO WHAT I WANT YOU TO DO IS I WANT YOU TO GO INTO A equals 50.
So we create a variable of an integer type and we set its value to 50.
Now I want you to go if a equals equals 50.
Make sure you do.
Equals equals because equals this assignment equals equals is comparison.
So if A is equal to 50 then print half a is equal to 50.
OK.
So that is an F statement.
So now if we now compile our program by copying the path go into command prompt like before.
See the right click enter and we do GCSE main that C dash show main and then we run Maine.
You can see a is equal to 50.
Now change this value to forty nine or 40 or whatever as long as it's not 50 right now.
I know what you said I not want you to compile that again.
I'm press type Maine again and you see nothing I put it because what happens is it goes in here and
it says is equal to 50.
No it isn't equal to 50 do nothing.
That's what happens.
But now let's try this.
Else if a equals 40 equals equals 40.
So then if we print f A is equal to 40.
So what will happen here is it will say is a equal to 50.
No it isn't.
OK.
Then is equal to 40.
Yes it is.
Run this code.
So if we now compile that and we do mean you can see I use equal to 40.
If you now do.
Else if A is equal to 30 and you do print f A is equal to 30 then if A is equal to 30 that will be run.
But if A is equal to none of these and nothing will happen.
So if we do thirty five in here and we compile that nothing happens.
But now you can do an.
Else.
So what this does if A is equal to 50 output A is equal to 50.
If A is equal to four the output is equal to 40.
If A is equal 30 output A is equal to 30.
If it's equal to none of these then run the code in here which will be printed if none of the above.
So now if you now compile that and do mean none of the above is show.
So that is if statements and you can have an.
Else with only one if just like this.
So we can say if a is equal to 50 print that.
Else print that.
So now if we can pile up none of the above is shown but if we set a 50 and we compile that A is equal
to 50.
So that is if statements for you in the C programming language very handy a necessary feature.
Now I want to make the distinction between expressions and statements.
So what I want you to do I want it to go integer.
B equals a equals equals 50.
Now what happens here then.
Well when we said there will be we say is a equal equal to 50.
If it is this is replaced with one.
Otherwise it's replaced with zero.
And this is done at runtime by the way.
So if we now go print f I print an integer and we pass and b we now run that GCSE and we do mean you
see one.
I would put it.
Now change.
Change a to thirty and run that again and you see zero.
So I want to make that distinction because this
if statements are an f key word and an expression.
So this here that I've highlighted is not it's not an if statement it isn't and I want to make that
very clear.
So what we do here is entropy equals a equals equals fifty B becomes one if a is equal to fifty or becomes
zero if it isn't.
So that was an important distinction to make.
Now watch this extra to equals 1.
And now if you do if B and we go in here print f b is true.
Okay.
Now if you can Paula you can see B is true.
Now if B is equal to zero and you compile that nothing is shown.
So essentially the if statement if the value pass here ends up equivocating to true which is above why
above zero then B is true.
If you put 50 here and try that compile that B is true.
So if it's not zero B is true.
If you put in zero in here compile that nothing show.
So the s statement says if this expression equivalence to true which is anything above zero then B is
true.
So I hope that sums up the expressions there.
Now there are special things you can do to sort of flip true to false and false to true.
So how does that work then.
Well if you say B equals one and in here you do explanation Mark B.
And I want you to run that.
You can see nothing's displayed but if you now change b to zero and you compile that and run that B
is true.
So this operator here flips it from from true to false often false to true.
That's what that operator does.
And again it can be used here.
If you set one test if you set a to one and we're going to print out B again one second and you now
go explanation mark a b equals explanation mark a few compile.
Now you can see B is zero.
But now if you set a to zero.
Can I see B is one.
So how does this work.
Well because A is zero.
We use this special operator here which flips 0 to 1 which is true.
So I hope that explains if statements please to go back through the lecture slowly and listen again
if you didn't quite understand or send me a message about that.