Now, we will take two quizzes
regarding nested loops and conditional branches.
In your opinion, what will this code print upon execution ?
The correct answer is D.
Let us see why.
The code starts with a first
instruction "for" declaring a variable i
and initializing it to 0.
The condition is "i strictly less than 3".
The increment statement is " ++i ".
i will thus take the values 0, 1 and 2.
The first time we enter the loop, i has the value 0.
Now, we move on to this second instruction "for",
which is inside the first loop,
declaring a variable j, initialized to 0.
The condition is "j stricly less than 4".
The increment statement is " ++j ".
Thus, j will take the values 0, 1, 2 and 3.
The first time we enter the "for j" loop,
j will have the value 0.
We will execute the instructions
inside the "for j" loop, starting
with this conditional statement, testing the condition "i == j ".
i is 0.
j is 0 aswell.
The condition is thus true and we will execute this instruction,
printing an asterisk.
By the way, for this printing here,
we are using a "print", not a "println", which means there is no line break.
We skip the "else" part of the conditional statement.
We reach the end of the "for j" loop and jump back here.
j will take
the value 1 and we keep going inside the "for j" loop.
We will now execute the conditional statement testing the condition "i == j".
i is 0. But this time, j is 1.
The condition is thus false and we will execute the
"else" part of the conditional statement, that is, printing
j's value : 1. We will print it here.
Now we reach the end of the "for j" loop and come back
here again. j will take the
value 2.
And we will print 2.
Then, j will take the value 3.
And we will print 3.
Now we exit the "for j" loop.
We reach this instruction,
generating a line break.
Therefore, the next printing will occur here.
We now reach the end of the "for i" loop.
We come back here.
i will take the value 1 and we
will reexecute the instructions
inside the "for i" loop. Therefore, restarting
here, j being initialized to 0 again.
We execute the instructions inside the "for j" loop.
We test the condition "is i equal to j?".
i is 1. j is 0. The condition is thus false.
We will thus print j's value here, namely 0.
Now, we will come back here, with j's value being 1.
We keep going inside the "for j" loop, executing this conditional statement.
We test the condition "i == j", This time, i is 1.
j is 1 aswell. Thus, the condition is true.
We can execute this part of the conditional statement,
thus printing an asterisk. Now we come back here,
continuing inside the "for j" loop. Now you can see
that we have indeed printed this, here.
.
Now, this second quiz is a bit more difficult.
Please note that here, we're using the value of the variable
i, declared in 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" declares a variable i, initialized to 0.
The condition is "i strictly less than 3".
The increment statement is " ++i".
Once again, i will take the values 0, 1 and 2.
We begin by entering the "for i" loop,
with i's value being 0. Now we execute this instruction,
a second "for loop", initializing a variable j to 0.
The condition is "j strictly less than i".
i is 0. j is 0 aswell.
Thus, the condition is false.