In this lecture you will learn about the conditional statements.
So conditional statements allows you to make decisions in your programs.
So these decisions rest on boolean expressions so which evaluate a true or false.
So for example, you can compare some operators such as greater than or not equal to world 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 named X here and assign it to zero and 42 equals here.
I will I will write all of these quotes here and we'll explain later here.
42 not equals to zero.
Here and the 100 greater than 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 let me write this here.
This is the equality operator.
It will return boolean or false.
This is the inequality operator.
Inequality.
And this is here.
Actually, let me use the tabs here.
Okay.
One, two.
Three.
This is a unique operator here.
This is greater than and.
This is less greater than.
Greater than or equal to.
Here.
This is less than and this is less than or equal to operator.
Here.
So this program produces no output.
Let's compile it and show what it will produce.
This will return zero because we are not apparent.
We are printing nothing here and using no variables here.
As you can see, Sea Lion automatically assign them, assign not the note them as unused variables.
So while the program doesn't produce any output compiling, it helps to verify that you are written
as valid plus plus code.
So to generate more interesting programs, you use a conditional statements like if here so an 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 if statement.
But basic logic here is you use the boolean expression question and then statement.
Your statement.
Or, for example, statement here.
So as Boolean expression, let's create a bool type here.
Bool boolean expression.
Test.
Test here.
Assign it true.
So if this boolean expression is true.
Boolean 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, um, or let's just use print f.
We're.
If a print f.
We have to enter the variable here.
Okay.
We use this studio here because this printf contains this printf has this studio has the printf function
in it.
So this, as you know, printf is not default, just system method in C plus plus here.
So let's create an executed or.
Yeah.
X 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 another if statement.
You can write this like compound statement because sometimes you will want to a group of statements
to run rather than a single statement.
If you do 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 braces here like this
so you can use compound statements within within the if statements as follows here.
So let's create an A here or let's create another Boolean expression.
So if 100 is Boolean, be here.
If 100 is greater than it, 100 is greater than X here.
Here.
Let's make it look nice.
Here.
Here and here.
So.
Uh, okay.
So if B, our B variable is if this expression will return the 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.
A, if B.
Is true, evaluates true.
Then print F this year for example.
Code.
Executed.
Printf.
Executed.
But to.
Here code one and executed code two.
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 two.
So if the comment is, as you know, the compiler will not compile this code and use this as a comment,
just a text here.
As you can see, the both of our functions, both of our expressions and quotes is executed in this
if statement.
So if we if this value is 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 get the same output.
So if we change to use less than operator here, this function, this code will not x 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 will not execute in this time.
So you can elaborate the if statement using the else if and else statements here.
So these optional additions 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 and New Line.
And here else if.
Our my statement.
My statement is true.
My statement.
This is a this is a Boolean expression we have to give here if false or let's actually use the create
a new variable integer.
My var variable here is 100 and integer.
My my.
My car here is under and my van here is 50 and integer my.
My phone is, for example, ten.
This is just an example variable names.
So if our my car.
Is greater than zero.
But 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, use the.
My car and give the second statement if my car equals greater than.
Uh, actually, let's make this 50 and my car is greater than 50.
And then you give this my car greater than 100.
Here.
Hundred.
And then else.
And we else we don't have to give condition here.
Then if these conditions will not execute, then use this else condition here and we're going to write
it here.
We don't know about what or just evaluates.
S statement here.
If and else if statements does not executed here and our comment is executed here and use this here.
So let's run this program and executed code my car greater than 50 here.
As you can see here, our car, my car is equals to zero and my car is equals to 100.
And that's why this else 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 delete the URLs here and use this.
And as you can see, both of our expression expressions will be executed here.
We'll make it nice.
So 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 the 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 statement two will be
executed.
Otherwise.
Statement three will be executed.
So if you make it like one, then our.
Executed code two here actually if integer mcar.
And else.
Else if.
Here.
So you can include any number of else if classes or omit them entirely.
So as with the initial if statement, the boolean expression or for each else if class is evaluated
in order.
So when one of these boolean expression evaluates to true evaluation stops and corresponding statement
executes.
So if no else if evaluates to true the else here, statement three will always execute.
So now I want to write some code here.
So integer create an integer here or let's value make my card variable zero int here.
Then if our my card greater than zero then here print f my car, my car greater than zero here or here.
And else if here.
If my car less than zero, then print f.
Print F negative.
My car.
Less than zero.
And else statement here.
My car print f my car equals to zero.
So now we can run this program and see what happens.
Actually, let me.
So as you can see in this case, my card is zero.
So if we assign and this if statement is executed, as you can see here.
So if we make it more my card to.
Then my car greater than zero will execute as well if we make it minus one.
Then my card less than zero.
Here.
This statement is executed.
If you make it zero again, then the else statement my car equals to zero will be executed.
And that's it.
About the C plus plus conditional statements here.