Forums Register Login

Evaluation of nested loops

+Pie Number of slices to send: Send
Hello, All

Another semester is upon us. I will teach Java Programming at my college again this semester (as well as C and C++)

I am looking for tips and tricks to show my students how to evaluate nested loops like


for (etc,,)
for (etc)

I like to show how the code actually does work by tracing throught the code. AS you know it can get confusing quickly.

I thought somewhere here or on another board someone posted a good shortcut for evaluating nested loops. Was there one in SCJP books?

I have tried google. Not much there to help.

Thanks for the ideas

JC





__________________

+Pie Number of slices to send: Send
something like:
+Pie Number of slices to send: Send
 

Originally posted by James Chegwidden:
I am looking for tips and tricks to show my students how to evaluate nested loops like


for (etc,,)
for (etc)

I like to show how the code actually does work by tracing throught the code. AS you know it can get confusing quickly.



Sounds like fun... Here some code to play with...



When all the loop finish, the total number of iterations is displayed... and your students can try to predict what the value is...

have fun...

(Marilyn moved some comments to shorten lines)
[ August 14, 2004: Message edited by: Marilyn de Queiroz ]
+Pie Number of slices to send: Send
I was actually looking for a quick way to evaluate nested loops by hand. Such as when you have to take the SCJP exam and have only your paper and pencil in hand.

I thought I read somewhere using a matrix with usign the row and columns and placing a check, line, or X through possible choices.

Any thoughts.

JC
+Pie Number of slices to send: Send
When I was doing SCJP mock tests, etc. when my brain had turned to jam I would write out the variable names as column headings. It's particularly useful for those loop examples that contain a switch with lots of ++, -- and missing breaks.

I guess the matrix might be appropriate for absolute beginners but I would have thought it would be too time-consuming to bother for SCJP. Also relies on known break conditions.

Jules
+Pie Number of slices to send: Send
 

Originally posted by Julian Kennedy:
When I was doing SCJP mock tests, etc. when my brain had turned to jam I would write out the variable names as column headings. It's particularly useful for those loop examples that contain a switch with lots of ++, -- and missing breaks.

I guess the matrix might be appropriate for absolute beginners but I would have thought it would be too time-consuming to bother for SCJP. Also relies on known break conditions.

Jules



I do the same. The variable names are the column headings and each row represents a loop iteration.

James, If you are looking for a good workbook to use along with any Java fundamentals textbook, then my book would be a good choice. Please click on the link in my signature.
+Pie Number of slices to send: Send
I should have also mentioned that my book just became available on Amazon.com.

http://www.amazon.com/exec/obidos/tg/detail/-/097458620X/qid=1092526859/sr=1-1/ref=sr_1_1/104-8939810-1179143?v=glance&s=books

The book is printed and distributed by booksurgepublishing.com. That's why the booksurge web page has a little more information than the Amazon page. The book cover image and other information should be on the Amazon page within the next couple weeks.
+Pie Number of slices to send: Send
Dan,

I will look at it. Every semester I teach Java I give my students a list of Java Reference books and Websites. I include certification books. If it has tip and trick it I would definitely recommend it.

Since it not published with a major company, I can't get a free exam copy can I?

JC
+Pie Number of slices to send: Send
James,

I think that the most useful feature of my book is the detailed explanations of the answers. The chapters of the book are organized such as those of the Study Guide 1 exam set on my web site. Each of those chapters begins with an introduction to the material covered by the mock exam in that chapter. Since the book is intended to serve as a workbook to accompany any Java fundamentals textbook, the chapter introductions are not intended to replace any Java fundamentals textbook. My book also contains some helpful appendicies.

My book contains 398 pages with very narrow margins and text printed in a small font. The dimensions of my book are 8.25 inches by 10.5 inches. Most Java textbooks today are printed on smaller pages with very wide margins and large fonts. If I had used a more typical page size and margin widths, the book might have contained twice as many pages. In other words, there is a lot of content packed into those 398 pages.

Since my book is self published, the cost of any sample copies comes out of my pocket. I can give you a sample chapter at no charge. Just click on that link.
+Pie Number of slices to send: Send
It might be interesting to "unwind" the loops and set up a problem and solution without loops. For example, number all the squares on a 3x3 checkerboard:

Hmmm, I guess start with a 1 dimensional problem first, solve it the long way then with a loop. Then seeing the 2 dim problem newbies ought to be able to spot the first dimension holding steady while a very similar loop goes through the second dimension.

That make any sense?

(Off topic - speaking of a 3x3 board, anybody know the game Hexapawn and the Matchbox Computer?
+Pie Number of slices to send: Send
No book list is complete without:
http://www.amazon.com/exec/obidos/ASIN/0596004656/qid=1092602592/sr=ka-1/ref=pd_ka_1/103-4454721-5019017
It's the one book I needed to read after doing 2semesters worth of Java - it made a lot of things a lot more clear!

Tom
+Pie Number of slices to send: Send
>

Originally posted by James Chegwidden:
>[QB]Hello, All

>Another semester is upon us. I will teach Java Programming at my college again >this semester (as well as C and C++)

>I am looking for tips and tricks to show my students how to evaluate nested >loops like

As a student at the beginner level in Computer Science, I'm trying to work out the same problem as you. My professor uses a column system where rows indicate iterations. However, I never understood what was going on until I understood how the loop structure worked itself. To do this I wrote a short program in C, simple enough that I could see how loops worked- simple because the relationships between the outputs and the inputs were clear and could not confuse.

#include <stdio.h>

int main()
{
int a, b, c;

for (a = 1; a <5 ; a++)
for (b=2; b<5 ; b++)
for (c=3; c<5 ; c++)
printf("%d %d %d\n", a,b,c);

return 0;
}

From comparing this and the output, I was able to figure it out...about 10 minutes ago!

-jake

 
If I'd had more time, I would have written a shorter letter. -T.S. Eliot such a short, tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1751 times.
Similar Threads
Any suggestions for software to teach Java?
Opinions wanted: better to start with structured constructs, or object concepts?
Teaching Java and OOP
Input/Output Question...
To Kathy Sierra, about teaching
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 14:13:59.