j takes the value 1 and we continue within the "for j" loop.
First executing the conditional statement
and testing the condition "i equal to j". i is 0.
This time, j is 1. The condition is thus false.
We execute this part of the conditional statement,
thus printing j's value.
So, we print 1 here.
We reach the end of the "for j" loop and, again,
jump back here.
j will now take the value 2.
We will print 2.
Then j will take the value 3 and we will print this 3.
Finally, we exit de "for loop" which iterates on j and
reach this instruction here,
triggering a line break.
Therefore, the next printing will occur here.
We reach the end of the "for i" loop
and jump back here.
i will take the value 1.
Now we execute these instructions again,
within this here "for loop".
We restart here and j will be initialized to 0 again.
We execute the instructions within the
"for j" loop and we test this condition :
"i equal to j". i has the value 1.
j has the value 0. The condition
is false and we thus print j's value,
that is, 0.
At this point, we jump back here and j will take the value 1.
We resume within the "for j" loop
and execute this conditional statement, namely the condition "i equal to j".
This time,
i has the value 1 and so does j. The condition is thus true.
And we can execute this part of the conditional statement :
printing an asterisk. We jump back here,
and continue within the "for j" loop. You should
now be convinced that this
will eventually be printed. The second quiz is
a little bit harder. Please note that, here, we're using
the value of the variable i, declared within the first "for loop".
In your opinion, what is the correct answer ?
The correct answer is A. Let us
explain why. As before,
the first "for loop" initializes a variable i to zero.
The condition is "i strictly less than 3".
And the increment statement is plus plus i.
Just as before, i will take the values 0, 1 and 2.
We begin by
entering the "for i" loop,
with i being 0. We execute this instruction here,
which is another "for loop" initializing a variable j to 0.
The condition is "j strictly less than i".
i is 0. j is 0 as well.
Thus this condition
is false. Thus we exit this loop
immediately and end up here.
In other words, we simply
generate a line break, which we will represent this way.
Now we jump back here. i will
take the value 1, j is
initialized to 0. The condition is
now "j strictly less than i" while i is 1.
Thus, j will take the value 0.
We will then exit the loop immediately.
For now, j is 0. We'll execute this instruction here.
That is, printing j's value : 0.
We exit the loop and reach
this instruction causing a line break.
We are now at the end of the "for i" loop and come back
here. i will take the value 2.
We enter the "for i" loop and execute this
instruction "for" which once again initiliazes j to 0.
The condition is now "j strictly less than 2".
Thus, j will take the values
0 and 1. We start with j,
having the value 0 and execute this "cout" instruction .
namely, we print j's value, that is 0.