0:00

In a previous video, we introduced

the conditional branch instruction, the if.

We also saw that in order to

work, this instruction needs to express conditions.

The conditions we used

until now were simple conditions, which

consisted of, for example, comparing the value of a variable with a value.

We are now going to see that in C++,

it is possible to express more complex conditions.

Here you see an example of a condition branch instructions, an

if, as we have already seen previously.

We can see that in order to write down this instruction, we need

to formulate a condition, as we can concretely see here.

The condition in the present case is a simple condition, which

consist in comparing the value of a variable n to the value 5.

Small syntactic detail : the condition in a conditional

branch instruction is always surrounded by

parentheses, as we can concretely see here.

A condition in C++ is in fact a sort of

expression whose particularity is to return two possible values.

These values are either "true", or "false".

Simply enough, a condition will be evaluated to "true" when

it is true, and evaluated to "false" when it is false.

Let's take a concrete example.

Let's imagine I have a variable n whose value is zero,

and I write the condition n smaller than five, that I want to evaluate.

Here the value of n, zero, is less than 5, which means

that this condition is true when I evaluate

it it will therefore return the value "true".

Now let's imagine n has a value of 10.

I evaluate the same expression. Obviously this isn't verified

anymore, the condition is false, therefore the evaluation

of the condition will return the value "false".

Let's begin by examining in a more exhaustive way

how to formulate simple conditions in C++.

The purpose of a simple condition is to compare two expressions.

and to do so we need to use comparison operators.

"Less than", "greater than" are two examples

which you already had the opportunity to see before.

So obviously C++ offers a whole

range of predefined operators to formulate simple

conditions. Let's imagine for example that I have two variables x, y. I want

to know if the value of x is less than the value of y. Naturally,

I'm going to use the "less than" operator

and write my condition like so.

Similarly, if I want to know if the value

of x is equal to the value of y, I am going to

use the comparison operator ==,

which lets me test this condition.

You have the operators "less than or equal", "greater or equal",

and "different than", which let you express all sorts of simple conditions.

Here's a little warning : in C++, the operator used to check

if two values are equal is defined by two "equal" symbols.

Don't confuse it with the = symbol,

which is used to formulate an assignment.

If I want to check if the value of a variable n is 5, I'm going

to use the operator == to formulate my

condition, and not the simple equal operator.

You must be aware however that this way of writing a condition

is considered licit by C++ compilers, we

are going to examine why in the next slide.

Licit indeed, yet discouraged, at

this stage of learning at least.

What you must know is that in C++, every expression

whatsoever does something, but also has a value.

Let's examine this wtih a concrete example.

Here we have an expression, n =

5, this expression clearly does something.

What it does is assigning to a variable n the

value 5.

In C++, this expression also has a value.

It's value is the the value of n after the assignment.

Thus, the expression "n = 5" has value 5.

That is why it is perfectly licit to write something

like this : m = n = 5,

5, which aims to assign to a

variable m the value of this expression, that is 5.

It is

therefore totally correct to write something like this : "if (n = 5)"

which is the same as writing "if (5)".

Knowing that in C++, every non-zero value is

equivalent to "true", this condition will always be evaluated as true.

Note that in terms of best practices,

it is totally discouraged to do it this

way. I have shown you this in detail

and explicitly only because to confuse == and = to

perform a comparison is a mistake beginners often make.

So in absolute, this way of doing it is to

avoid, even if it is tolerated by the compiler.

Now let's illustrate the use of simple conditions in C++

on small examples.

Here we have a small program that begins by declaring two

variables a and b and affects initial values to them, respectively 1 and 2.

We then encounter a conditional branch instruction, which tests

if the value of a is the same as the value of b with a simple condition.

This is done with the comparison operator ==.

Since this

condition isn't verified, the result of the condition's evaluation

will be the value "false", which will result in

branching the execution on the else block of the if instruction, therefore

causing to print the message "cas 2", which we can see here.

We then continue the execution of the sequences,

and we encounter a second conditional

branch instruction, which will this time compare the

value of two times a to the value of b.

By the way, we can here observe that the comparison

operators not only allow to compare the values of

two variables, as it is the case here, but also

the values of two expressions, in a more general sense.

Here if we evaluate two times a, we obtain

the value 2, which is the same as the value

of b, and the result of the condition's evaluation will therefore

be "true", which will have as a consequence to branch

the execution on the positive block, the true block of the if instruction.

This will cause the message "b est égal au

double de a" to be printed, as can be seen here.

In terms of good practices, we here have a relatively

simple expression. You will however note that it is recommended,

when the expression becomes more complex, to use parentheses, as

we could already do here, by adding parentheses around, for example, this

part of the expression, which makes the expression more readable.

We have just seen how to use

simple conditions by using comparison operators.

In practice, it is often necessary to combine many

of these simple conditions to formulate a more complex condition.

To combine simple expressions, we need

to use another type of operator, the logical operators.

For example, the logical operator AND, which is defined in C++ either

by the key-word "and", or by the symbol

"&&", is an operator operating with two operands, which are

two logical expressions, that is to say

expressions returning either "true" or "false".

The result of the evaluation involving the and is "true"

only in the case where each operand has the value "true".

In every other case, let's say for example that one of the two operands, and a

fortiori the two, are "false", the result of

the global expression's evaluation will also be "false", here.

Now let's have a look, with a concrete example, at how to use the logical

operator "and".

Here an example of a small

program, which asks the user to enter a

number, and we want this number to be between 1 and 10.

The number is read from the standard input, and stored in a variable n.

We want to verify that the number entered by

the user meets our expectations, is between

the boundary numbers in question, and that's why

the two following conditions must

simultaneously verified.

The entered number must be greater than or

equal to 1, and less than or equal to 10.

Since we want the two conditions to be

simultaneously verified, we must use the logical operator AND.

Now let's imagine that the user has entered the

value zero, this will mean that

this expression's evaluation will return "false". An

expression that involves an and returns "true"

only if its two operands are "true", which isn't the case here.

So here, the result of the evaluation of the

expression with the and is also "false".

As a result, we will branch on the

else part of the if, and print "incorrect"

to show the user that he hasn't

entered a number according to our expectations.

Contrariwise, if the user now enters

the value 5, this expression's evaluation

will return "true", the evaluation of this second

expression will also return "true", which

will mean that the evaluation of

the expression with the and also returns "true".

At this moment, we will branch on the positive part of the if and print

"correct" to tell the user that the

entered number is indeed correct.

Another usual logical operator, the OR, which

is defined in C++ either by the reserved word or,

or by a double vertical bar.

As with the logical operator AND, the logical operator OR requires two

operands, which are the logical

expressions, meaning that they return "true" or "false".

The expression's evaluation with an OR returns "true" if one or the other,

not necessarily the two, one or the other of the operands is "true".

It is therefore quite clear that the only situation or logical

expression involving and OR returning "false" is

a situation where the two operands return "false".

In this case, the expression with the OR also returns "false".

Now let's illustrate the use of the logical OR with a small concrete example.

Here we have a small program

which asks the user to choose two values.

These two values are entered from the standard input and are stored

in two variables m and n.

In the present case, we want one or the other

of them to be positive, as we only need

one of the two values to be positive.

We test these conditions by using the logical operator OR.

Initially, let's suppose that the user

enters the values 1 and -1.

In this case, the first operand

will be evaluated as "true", because it is indeed positive, and therefore

the expression's evaluation returns "true", as it is sufficient

that one or the other of the operands be "true"

for the result to be "true" for the global expression.

Therefore, it is indeed the message "au moins

une valeur est positive" that will be printed in the present case.

Now let's suppose that the entered values are

-1 and 1.

In this case, the evaluation of this first

expression will return "false", and the evaluation of the second

expression, on the other hand, will return "true", as it

sufficient that one or the other of the operands

be "true" for all of it to be "true".

The global expression's evaluation returns "true", and

once again the message "au moins une valeur est positive" is printed.

Last case : let's imagine that the user enters

the values minus 1 and -1 for each variable m and n. In this

case, the evaluation of the first expression

will return "false", the evaluation of the second expression

will also return "false", as the two operands

are evaluated as "false", the global expression also returns

"false" and in this case, it's the message

"les deux valeurs sont négatives" that will be printed.

Last usual logical operator, the NOT, the negation, which is

defined in C++ either by the reserved word not, or by the exclamation mark.

The logical operator NOT is a

unary logical operator, which means that it expects only

one operand, which is once again

a logical expression returning "true" or "false".

The purpose of the logical operator NOT is to return the negation

of the value of its operand.

For example, let's suppose that the operand is evaluated as

"false", which means that a is greater than or

equal to b, the global expression's evaluation with the

not will return the negation of "false", namely "true".

Now let's suppose that the evaluation of this operand

returns "true", the global expression will this time return the

negation of "true", namely "false".

We'll have the opportunity to see many examples of the use

of the operator NOT in the following chapters of this course.