In this lecture you will learn about the conditional statements.
So a conditional statements allows you to make decisions in your programs.
So these decisions rest on building expressions.
So which evaluate a true or false.
So for example, you can compare some operators such as greater than or not equal to build to build
a Boolean expression.
So some basic comparison operators.
Let's create an here, for example, integer.
Let's create an integer value variable.
Name x here and assign it to zero and 42 equals here.
I will I will write all of this codes here and we'll explain later here.
4 to 2, not equals to zero.
Here.
And 109 X here.
One, two, three.
Greater than or equal to X here.
And minus ten.
Less than X here.
And -99 less than or equal to X here.
So let's let me write this here.
This is the equality operator.
Bit further term boolean or false.
This is the inequality operator Inequality.
And this is his actual allotment.
Use the tabs here.
Okay.
One, two.
This is a nuclear operator here.
This is greater than.
And the.
This is less greater than.
Greater than or equal to.
Here.
This says less than and this is less than or equal to.
Operator.
Click here.
So this program produce no output.
Let's compile it and show what it will produce.
This will return zero because we are not a parent.
We are printing nothing here and using no variables here, as you can see.
See lion automatically assign them, assign not them, not them as unused variables.
So while the program doesn't produce any output compiling it helps to verify that your written is valid.
C++ code.
So to generate more interesting programs, you use conditional statements like if your SO and if statement
contains a boolean expression and one or more nested statements.
So depending on whether the boolean value is a true or false, so the program decides which nested statement
to execute.
So there are several forms of statement, but basic logic here is you use a boolean expression version
and then statement.
Your statement.
Or, for example, statement here.
So as an expression, let's create a bull type here.
Boolean expression.
Test.
Test here.
Assign it true.
So if this boolean expression is true.
Purely an expression.
So if this boolean expression is true, then our statement will be executed.
So as a boolean expression.
So let's create a bool here.
A You can write this as a boolean expression here, so let's put it here.
And as you can see here, we got no error here.
So if here print F or C out STD or let's just use print F.
Yeah.
If a f.
We have to enter the variable here.
We use this studio here because this printf contains this, printf has this studio has the printer function
in it.
So this, as you know, printf is not default just system method in C++ here.
So let's create create an executed or.
Yeah.
Thanks.
Here.
And let's run this code.
As you can see, we got no output here because our Boolean expression is false, as you can see here.
And if we put this, if 42 equals 42, this will evaluate true.
That's why our code will be executed.
Here we have another point.
Another if statement.
You can write this like compound statement because sometimes you will want a group of statements to
run rather than a single statement.
If you do a single statement, you can use this as follows.
So such a group is called the compound statement.
So to declare the compound statement, simply wrap the group of statements in a brazier like this so
you can use compound statements with within the if statements as follows here.
So let's create an A here or let's get another Boolean expression.
So if 100 is a Boolean, be here, if 100 is greater than
100 is greater than X.
Here, here.
Let's make it look nice.
Yes.
Here and here.
So.
Okay.
So if B will our B variable is if this expression will return the grid true, then our if function if
statement, if statement will be executed.
If not, this is not a function.
This is a statement here.
So let's create let's give this as an example.
If B.
Is true.
If it's true, then printed this year, for example, code.
Executed.
S.F. executed.
Quote to.
Pier, code one and executed code here and use this.
So let's use this.
And as you can see here, let's actually add new line.
So make it look nice.
Here.
So executed Code one.
Executed code two.
So in this case, our both this statement is executed.
So actually, let's make this comment to.
So if the comment is, as you know, the compiler will not compile this code and use this as a command,
just a text here.
As you can see, the both of our functions, both of our expressions and codes is executed in this if
statement.
So if we if this values true, or we can use this and directly give this as an here, if 100 is greater
than X, then execute our code.
As you can see, we got that same output.
So if we change to use less than operator here, this function, this code will not this if statement
will not execute.
As you can see here, the x equals zero.
So our 100 is not less than zero.
And this if statement, we will not execute in this time.
So you can elaborate if statement using the ls if and l statements here.
So this optional addition allows you to describe more complicated branching behavior.
So let's create another example here.
If so, make it here.
And if our boolean expression is true, then use the statement.
Print F.
My statement.
One new line.
And here else if.
Our my statement.
My statement is true.
My statement.
This is this is an expression we have to give here if false or let's actually use the create a new variable
integer.
My var variable here is hundred and integer.
My my.
My car and my van here is 50 and integer.
My.
My phone is, for example, ten, just as an example variable names.
So if our my car.
Is greater than zero.
Actually, we don't need these variables here.
Then evaluate my car.
Greater than greater than 100.
Here.
And then if this statement is not executed, you lose the.
My car and give the second statement.
If my car equals greater than actually make this 50 and my car is greater than 50, and then you give
this my car.
Greater than 100.
Yeah.
Hundreds.
And then else and else.
We don't have to give condition here.
Then if these conditions will not exceed good, then use this else condition here and you're going to
write it here.
We don't know about what or just values.
As statement here.
If and else if statements does not executed here and our SE comment is executed here and use this here.
So let's run this program and execute it code my car greater than 50 here as you can see here, our
car my car is equal to zero.
My car is equal to 100.
And that's why this ls If command is not executed.
And let's create an let's assign my car 101.
And as you can see here, my car is greater than 50 here.
So if we do like the little else here and use this and as you can see, both of our expression expressions
will be executed here.
Make it nice.
So like my car greater than 50, my car greater than 100.
So the first boolean expression here is evaluated here.
Actually, let's make it evaluated.
So if boolean expression one is true and then statement one is evaluated and if statement stops executed
and if boolean expression one is false, then boolean expression two is evaluated.
So.
If true this statement to hear.
If boolean expression two is true, then the our statement two will be executed.
Otherwise statement free will be executed.
So if you make it like one, then our.
Excluded.
Court to hear actually if integer my car and else else if.
Here, so you can include any number of URLs if classes.
Or omit them entirely.
So as with the initial if statement, the boolean expression for each else if class is evaluated in
order.
So when one of these boolean exploration evaluates to true evaluation stops and corresponding statement
executes.
So if no else if course evaluates to true the else here statement tree will always execute.
So now I want to write some quote here.
So integer, create an integer here or let's value make my card variable zero in here.
Then if our my car greater than zero, then here print my car, my car greater than zero here or less
here.
And as if here.
If my car less than zero then print.
Print test.
Negative.
My car.
Less than zero.
And F statement here.
My car print off my car equals to zero.
So now we can run this program and see what happens.
Let me.
So as you can see in this case, my card is zero.
So if a sign and this if statement is executed, as you can see here.
So if we make it more why my car to.
Then my car greater than zero, will execute us well if we make it minus one.
Then my card less than zero here.
This statement is executed.
If I make it zero again, then the SE statement my car equals to zero will be executed.
And that's it.
But the C++ conditional statements here.