Now let's have a look at various mistakes often made by
beginner programmers, when it comes to writing conditional branches.
The first trap that awaits you is a trap into
which probably every beginner has fallen,
it's the fact that the equality test is written with two equal
symbols, and not a unique symbol, which is itself for the assignement.
For example, this code here
is accepted by the compiler, but won't test if one is equal to one.
To achieve this, we should have written a equal equal one.
Moreover, it will assign the value one to the variable a.
So, even if the compiler
accepts this code, most of the compilers will display a warning.
That is, a warning message.
For example, g plus plus, with the option Wall, will
print this message here, or this one here, if you have a french version of the compiler.
So, if you get one of these two messages, you
will know that you too have fallen in this trap.
Perhaps, you see the error in this new piece of code, there
shouldn't be two semicolons, here.
And, if you put all the same a semicolon, the message "a
vaut un" will be printed, whatever the value of a.
So why ?
Because in fact, this semicolon is
considered as an instruction, an instruction
that does nothing.
And this code will be interpreted in the following
way, that is that the semicolon,
this instruction that does
nothing, will be considered within the conditional branch, and
the instruction cout will be considered only after the conditional branch.
This means that if a has the value one, we
execute the empty instruction, that is, we do nothing.
And whatever the value of a, execute
the instruction cout, that is, to print the message "a vaut un".
And watch out, in this case the compiler doesn't give any warning during the
compilation. You'll note that if we had used curly braces, there would have
been a single instruction in the conditional branch, and the error we have just
discussed would have less likely happened.
It is therefore a good idea to systematically use curly braces, even
when there's only one instruction in the block.
A third possible mistake is precisely
to forget the curly braces, in the same way as in this code.
Having indented these lines, that is,
to have shifted them slightly to the
right, is not sufficient for them to be considered as
inside the conditional branch, unlike
other languages.
In your opinion, what happens if I try and compile this code ?
Well, the compiler will print this warning message, meaning that he
thinks that there's a syntax error, before the key-word else.
So, why ?
That's because, in fact, this code is
interpreted in the same way as this code here.
That is to say, the first cout instruction, here, is considered
as being within the conditional branch.
This is what I represented here.
But this second instruction is
considered as being after the conditional branch.
This is what I represented here.
The compiler then comes upon the key-word else,
and, from its perspective, there isn't any if instruction
related to this else key-word,
as we have already left the conditional branch.
It therefore thinks that there's a syntax error around this part of the program,
and will print this error message. Now let's do three quizzes.
First of all, what does this code print when it is executed ?
And when the user enters the value one for the
variable n, and the value two for the variable b.
In your opinion,
what's the correct answer ? The correct answer is the answer C.
Let's have a look why.
If you think you've understood, you can go directly to the next quiz.
So, let me remind you that, in order for a condition with an
and to be true, the two operands must be true.
And in order for a condition with an or to be true, it is sufficient for
only one of the two operands to be true.
I said earlier that the user enters one for n, and two for p.
This condition is therefore true, because one is strictly less than two.
This expression, two times n, simply has the value two.
Two is greater than or equal to two. Therefore, this condition is true.
So the overall expression is, here, a condition that is true.
We are therefore going to enter the conditional branch,
execute this cout instruction, which prints one, one.
We then go to this conditional branch.
This condition is, well, the same as before, it is still true.
The overall condition
uses an or.
Therefore, I don't need to look at the value of this condition.
I know that this overall condition is true.
And as it is true, well, we
execute this instruction, which prints a two.
We then advance to this third conditional branch,
which uses, once again, the same condition.
We know that this condition is true.
We are therefore going to
enter the conditional branch, and execute this part.
This expression has the value two.
p still has the value two.
Therefore, this condition is true.
We are therefore going to execute this part
of the conditional branch, and print three.
We are then going to arrive at the end of this conditional branch,
move to this instruction, which simply prints an newline.
Another exercise, another code.
This time, the user enters the values one for n, and three for p.
What is going to be printed ?
The correct answer is the answer B. And we are going to have a look why.
This time, the user enters one for n, and three
for p. This condition is therefore true, as one
is strictly less than three. This expression two times
n has the value two, p has the value three. Therefore, this condition is false.
As this condition uses a single
and, this condition is false.
We are consequently going to skip the conditional branch, and move on.
This condition is still true, like before.
The overall condition uses one or.
Thus, I don't need to consider the value of this condition.
I know that whatever its value,
this entire expression, this entire condition, is true.
We are therefore going to
enter the conditional branch, and print two.
We then move on to this conditional branch.
This condition is true, once again, so we enter
the conditional branch to execute this part.
Therefore, this expression has the value two, p has the value three.
Thus I already know that this condition is false.
As this
condition is false, we move on to this
part of the conditional branch, and print four.
We arrive at the end of this conditional branch, to then
move on to the cout endl, which simply prints an newline.
One last time, still the same code.
This time, the user enter the values two for n, and one
for p. Which is the correct answer ?
It's the answer A.
For this last time, the user enters the value
two for n, and the value one for p.
This condition is therefore false, as two isn't strictly less than one.
As the overall condition uses an
and, I don't have to consider
the value of this condition. I know that it's all necesseraly false.
We therefore skip this first conditional branch.
This condition is always false.
On the other side, I this time use an or.
Therefore, I am obliged to consider the value of this condition.
This expression has the value two times n. That is, two times
two, so four. p has the value one.
Thus, this condition is true, as four is greater than or equal to one.
Therefore, the overall condition is true.
We are therefore going to execute this instruction, and print two.
We then move on to the next conditional branch.
This condition is always false.
We are therefore going to skip what is inside the conditional branch,
and move on to this part of the program, which simply prints an newline.
Let's end with the bool type, bool for boolean, which is the type of conditions.
This type will let us declare
variables which will contain the value of a condition.
A variable of
type bool is often called a boolean.
And a boolean has only two possible values : either true, or false. Or
more specifically, in C++, we are going to use the literal values, true and false.
In this example, I begin by declaring two variables of type int.
a which will be initialized with value one. b which will be
initialized with value two. In this third instruction,
I declare a variable which is called test un, which is of type bool.
Like with all variables, I can represent
test un with a box. And test un will be
initialized with the value of this condition : a equal b.
As a has the value one, and b has the value two, this condition is
false. test1 will therefore be initialized with the value
false
This last
instruction declares a variable test
deux, of type bool. That is, a boolean.
And test deux is going to be initialized with the value of this condition.
a has the value one, b has the value two. This condition is thus true.
test2 will therefore be initialized with the value true, or
more specifically, true, which means true in
English.
Some students have problems with
booleans, others sometimes have a mental block.
So, remember that a variable of type
bool is a variable like another.
That is, you can represent it as a box.
It turns out that this box can only contain two possible values,
either true, or false.
We can also use logical
operators, and, or, not, between variables of type bool, and we can also
use these variables as conditions in conditional branches,
and that's what we're going to have a look at in this example.
As in the previous example, I begin by
declaring a variable called a, of type
int, and initialized with the value one, and a
variable b, also of type int, initialized with the value
two. In this instruction, I declare a
variable of type bool, which is called c, and is initialized with
the value true. Then, here, I declare a
value of type bool, which I will call d, initialized with
the value of the condition "a equals b". a has the value 1, b has the value 2.
The condition is therefore false, and has the value false.
d is therefore initialized with the value false.