In this lecture you will learn about the conditional statements in C plus plus.
So the most frequently used conditional statements is if statement here if true, and there are code.
So in its simplest form, the if statement takes a logical expression true or false or we can do for
example is empty function like that.
This is not actually is empty function really but it we can take anything that returns a boolean value
and give this as parameter.
And if the parameter returns true, the if block will execute.
If not the.
And if not.
And we have else block or else if block the if block and else block will execute.
So if neither of if we only have the just one if block then if and our if here is false, then our code
will not execute at all.
So.
And we add here the logical expression in a pair of parentheses and immediately followed by the statement
that is executed if the condition is true.
So I want to write some quotes here.
For example, you cannot hear integer.
Integer A here STD sin.
Here we go.
We will get some.
Input from the keyboard here, from the user if a greater than ten then.
See out.
Here.
Um.
Okay.
A is greater than ten.
Here and.
As you can see.
Also, let's write our write, compile our code, and let's see what happens.
So we're going to type some value eight and we got no output because if Block was not true, so eight
is not greater than ten and this block use like it doesn't even there.
So this if block is passed.
So let's create again and let's run this program again and write, for example, 99.
As you can see here, A is greater than ten.
And this, um, this code executed.
So you can also use the else statement to catch the occasions when the condition is false.
So else here.
LC If this statement is false, then run else statement.
So for example, STD see out.
So if.
Hour.
A number is greater than ten then.
For example.
Within range here of.
Let's add here.
Here with him.
Range and STD and line.
Here.
For example, one and within range.
So or let's create a is less than than ten.
Here.
And let's try it for example five and we will get a is less than ten.
If I write 99 or for example, 11, we will get a is greater than ten and this code of block will execute.
So the condition is a logical expression, as I said earlier, and Cplusplus will convert from numeric
types to bool here.
So here, as I said earlier, zero is false and anything.
Anything non-zero is true.
Here.
So if you are not careful, this can be a source of an error that is not only difficult to notice,
but also can have an unexpected side effect.
So let's create an another code here.
Uh, for example.
Well, it's clear.
So.
We get, we create an integer, a get user, get input from user and write it to a and then if.
E equals.
Uh, a equals two minus one equals two.
Minus one.
Then.
We will print the message typed minus one.
Here and here We don't need for now the block.
So let's run.
Here.
Or let's write, let's use this and let's code here.
None minus one.
Here.
Let's write five.
We will get this minus one output here.
If we write minus one here, negative one, we will get types, negative one.
At which point after this, after we got this output, the loop finishes.
So if you mistype you may here end up with the code here.
For example, if a equals to minus one, then you will get this code here, for example, five typed
minus one.
So in this case, the assignment operator is used instead of equality.
Operator.
So I want to write it here.
This is a equality operator here.
Equality.
Equality Operator.
And this is the assignment.
Operator.
So make here.
Be careful when you use the operator because this mistakes is hard to notice, really.
So the result is that regardless of what you type at the console, the variable A is assigned to minus
one and the signs minus one is not zero.
The condition in the if statement is true.
Hence the rule clause of the statement is executed since the variable assigned to minus one.
This may alter the logic further on your code.
So the way to avoid this bug is to take advantage of the requirement that in an assignment of the left
hand side must be an l value.
Here, minus one.
Here.
A here.
Let's run this again.
99 and this is non-zero one.
So our code started to work correctly again.
So here the logical expression is minus one equal to operator A, and since this equal to operator is
a commutative, the order of the operands doesn't matter.
You get the same result here.
So it doesn't matter if you write a in left or right, you will get the same result here.
Minus one.
Let's write 89 here again you will your work will your code will work correctly.
So in this case here the assignment has an error value.
Here.
Assignment has an air value on the left hand side, and this will cause the compiler here.
An error if overwrite like that which error named value required as left operand of assignment.
So your code won't compile if you write like that.