Before we start, did you try solving the workbook yourself?
If not, please click the link in the resources folder and follow the instructions for this workbook.
In this solution, we're going to revisit Workbook 6.13.
Instead of printing our rows one by one, we're going to use a nested loop.
So here it tells us the outer loop needs to define this code.
Simple enough, we'll say four and I is equal to zero.
I is smaller than Price's dot length, so price is not.
Length is going to represent the number of rows three plus plus.
Where I is going to end up equaling zero one and two.
So let's copy this over.
When I equals zero, we're going to print baking.
When I equals one, we print a beverage, one, I equals two, we print cereals.
And now every time the outer loop runs through a particular row, we need an inner loop to run through
every single element in that row.
So here we'll say int j is equal to zero.
J is smaller than prices.
I length.
J plus plus.
So when the outer loop runs for one row, the inner loop is going to run through every single element
in that row.
And for every element we run through, we can print it.
And after the inner loop runs to completion, we print a new line.
Now, this might seem a little complex.
It might seem a bit scary, but don't worry, once we visualize the runtime, it's all going to make
a lot of sense.
So here we've got a two RD array.
The two RD array has three rows and each row has five elements.
The outer loop is going to start with equaling zero, and it's going to keep running so long as I is
smaller than the number of rows.
So we start at equaling zero case zero.
That means we're printing prices for baking.
All right.
And now each run from the outer loop is going to run the inner loop to completion.
So this one is going to start off at J equaling zero, and it will keep running so long as J is smaller
than the length of a row zero row I.
So J is going to start at zero.
Here I is zero and J is zero.
So we're printing 1299.
Here.
I still zero, of course, but J is one.
So we print 899.
I still zero Jace to 999.
I still zero J is three 1049.
i0j equals four 1199.
So I hope you can now see that each run from the outer loop runs the inner loop to completion.
And now that we've printed every single element in row zero, we can print a new line.
All right.
And now this is the second run of the outer loop I now equals one, which means now we're going to print
all of the prices for beverage.
And one run of the outer loop is going to run the inner loop to completion.
Once again, the inner loop starts off with J equaling zero.
The loop is going to keep running so long as J is smaller than the length of row one because here I
is one.
Here we index row 1j0 $0.99.
Row one element one 199.
Row one element 2 to 49.
Role.
One element three 149.
Row one element 4 to 99.
All right.
So now, after the inner loop runs to completion, we print a line of space.
And this is the third time that our outer loop runs.
This time I equals two, which means we're going to be printing all of the elements for row two.
Once again, each run of the outer loop is going to run the inner loop to completion.
J starts off at zero.
The loop is going to keep running so long as J is smaller than the length of RO two.
So long as J is smaller than five.
J starts off at zero.
Here we are indexing element zero 0 to 899.
Zero two element to one 790 902.
Element two 949.
Row two element three 999 and RO two element four 1099.
And after the outer loop runs the inner loop to completion, then we print a new line and now I a plus
plus increases I to three.
Three is not smaller than the number of rows three so our outer loop is going to break.
Press continue terminating the runtime and we are done.
I hope you enjoyed this breakpoint session.
No matter how complex something may seem once you put your breakpoints and you start visualizing the
runtime, it gives you a very clear picture of how your code is running.