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".