0:00

In a previous episode, we

introduced the conditional branch instruction if, and

we saw that in order to work,

such an instruction needs to express conditions.

The condition examples which we

saw until now were relatively simple, they

consisted for example in comparing the

value of a variable with another value.

We are now going to examine the notion of condition in a

more detailed way and see that, in Java, it is possible

to express relatively complex conditions.

So, you here have an example of a conditional

branching instruction, like we have already seen

previously, and we see that in order to write this conditional statement,

we need to express a condition, as we can see here.

Here the condition is a simple condition, which consists in comparing

the value of a variable n with a constant, the constant 5.

Small syntactic note :

the conditions in a conditional

statement are, in Java, always enclosed

in parentheses, which we concretely see here, in this small example.

Let's begin by examining in a more exhaustive

way how we can, in Java, express simple conditions,

The objective of simple conditions is to compare two expressions,

and for this effect we will use comparison operators.

We have already had the opportunity to come upon them several

times in examples, like less than or greater than.

Java, in fact, offers a whole collection of predefined

comparison operators, for example the less than operator lets us

compare if the value of an entity is less than the value of another entity.

Let's suppose for example that I have two variables x and y, and that I want to

know if the value of x is less than

the value of y, I would use the comparison operator less than

to perform this test, to express this condition.

In a similar way, I can use the operator ==

to verify if two entities have the same value.

For example, if I have once again two variables x

and y, if I want to test if the value of

x is the same as the value of y,

I would use == and would express my condition this way.

In a similar way, we also have the

operators greater than or equal to, less than or equal to, and different than.

What you must know is that the evaluation of such an expression

returns a boolean result, that is, the value "true"

for true, and the value "false"

for false. A small

warning : the comparison operator to test if

two values are equal is defined by two equal symbols.

Do not confuse it with the

unique = symbol, which represents assignment.

Here in this example, I test if the value of n is 5.

This isn't what I'm doing when I write n

= 5; n = 5 means that I assign the value

5 to the variable n.

It should be noted that this kind of

way of writing isn't tolerated by the compiler, in

Java, therefore here you'll have a compilation

failure, if you write this kind of instruction.

Now let's illustrate, with small concrete

examples, the use of simple conditions in Java.

Here we have the case of a small program which

begins by declaring two variables a and b, by

assigning to them initial values, respectively 1 and 2.

We now want to compare the values of a

and b, to check if they're equal,

and for this effect we have used a simple

condition which uses the comparison operator ==.

Here, the values of a and b are different, which

will has as a consequence that

the evaluation of this expression will return "false".

We will therefore branch on the else

part of the conditional statement, and execute this

instruction, which will have as a consequence to print the

message "Cas 2", as we see here.

Then let's continue the step by step execution,

and we now want to compare

the value of two times a with the value of b.

It should be noted that with simple conditions, we can compare not only

the value of two variables, as it is the case

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

If I evaluate two times a, I obviously get the value

2, the value 2 which is of course equal to

the value of b, so the evaluation of this

expression will return "true", this instruction,

the following instruction, will therefore

be executed, and "b est égal au

double de a" will be printed. Note here that we are dealing with an expression

that is relatively simple, when the expressions get more

complex, to formulate conditions, it

is recommended to use parentheses on the terms

of the expressions to make them more readable.

Here for example, we could have begun to do it

by adding a pair of parentheses around this term.

In another situation,

we now want to compare the values of a and

b to know if a is different from b.

As it is the case, the evaluation of this

expression will return "true", we will therefore

branch on the positive part of the if, the true part, and print "Cas 2".

If we continue in sequence, we now want to compare the value

of two times a with the value of b, to test if two

times a is different from b.

It isn't the case, so the evaluation of

this expression will return "false", therefore we will

branch the execution after the if instruction, as there isn't

any else block, so nothing gets printed.

We have just seen how it is possible to express

simple conditions in Java by using comparison operators.

In practice,

it is often necessary to formulate more complex

conditions, and therefore to combine simple conditions.

This is done with the help of logical operators.

For example, the AND logical operator, which in Java is defined

by the double symbol &&, lets us verify if two conditions are

simultaneously verified. We note that the logical operator

requires two operands. The evaluation

of the expression with the AND will return

"true" if and only if the evaluation

of each operand also returns "true".

Note that if suffices that one

of the two operands returns "false", for example this one,

and a fortiori the two, of course,

for the entire expression to also return "false".

Second concrete example on the use of the logical operand AND.

Here we have the case of

a small program, which asks the user to input

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 that the user

inputs is indeed in line with our expectations,

that is, that the number is between 1 and

10, we will consequently express a condition by

imposing to the user that the entered number is greater than

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

As each of these conditions

must be verified, we will use the AND logical operator.

Let's suppose initially that the user inputs a zero.

In this case,

the evaluation of the first operand will return "false".

We have seen previously that it suffices that one of the operands is "false"

for the entire expression with the AND logical operator to be "false",

we will therefore branch on the

else block of the conditional statement and

print "incorrect", to show the user

that the entered number doesn't fullfill

the requirement.

Now let's suppose that the user inputs the value

5, in this case the evaluation of this first

operand will return "true", as 5 is indeed

greater than or equal to 1, the evaluation

of this second operand will also return "true",

as 5 is indeed less than or

equal to 10, consequently, as the two

operands return "true", the entire expression will also

return "true", and in this case we branch on

the positive block, the true block of the conditional statement, and

print the message "correct", to tell the user that the

entered number fulfills our requirements indeed.

Other usual logical operator, the OR, which is defined in Java

by a double vertical bar.

In the same way as the AND logical operator,

the OR operator awaits two operands, which are

two logical expressions returning "true" or "false".

8:58

For the entire expression to return "true", one or

the other operand must return "true", a fortiori the two.

We see that in this case, if we want the logical expression

to return "false", the two operands must return "false".

In all the other cases, the expression will return "true".

Now, here's a small concrete illustration of the use of the OR logical operator,

let's suppose here that we have a small program which

asks the user to enter two values, and we want

one or the other of these values, at least, to be positive.

The two values are read on the standard input and

are stored in two variables n and m.

We want one or the other of the entered values

to be greater than zero, we shall therefore express a condition,

it is sufficient that either m is greater than or equal

to zero, or n is greater than or equal to zero.

As it is sufficient that one or the other of these conditions

is verified, we will naturally use the OR logical operator.

Let's suppose that the user

inputs the values -1 and 1, the evaluation

of this first operand will return "true", as

the value 1 is indeed greater than or equal to zero, and we have ween that it is

sufficient that one or the other of the operands is

"true" for the entire expression to also be "true",

therefore, we branch here on

the positive block of the if instruction, and tell

the user with a message, that indeed,

at least one of the entered values is positive.

Now let's suppose that the user has entered the values -1

and -1, that is, two negative values, in this case the evaluation

of this operand will obviously return "false", the evaluation of this second

operand also returns "false", and in this case, we're in the

situation when the evaluation of the global expression

returns "false", as the two operands are "false".

In this particular case, we will therefore

branch on the else block of the conditional statement,

and tell the user with a message

that none of the two entered values are positive.

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

which in Java is defined by an exclamation mark.

Unlike the two other operators we have just seen, the NOT operator,

the negation, is a unary operator,

that is, it awaits only one operand.

It's purpose is to return the negation of the value of this operand.

For example, let's suppose that the evaluation of this operand returns "false", which

concretely means that a would be greater than or equal to b, the negation

of the value "false" will be "true".

Now let's suppose that the evaluation of this operand returns on the contrary

"true", in this case, the negation of "true" will be "false".

We'll have the opportunity to examine many examples

of the use of the negation operator in the

next lessons.