aspose file tools*
The moose likes Beginning Java and the fly likes Evaluation of nested loops Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Evaluation of nested loops" Watch "Evaluation of nested loops" New topic
Author

Evaluation of nested loops

James Chegwidden
Author
Ranch Hand

Joined: Oct 06, 2002
Posts: 201
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





__________________



Author and Instructor, my book
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

something like:


http://home.arcor.de/hirnstrom/bewerbung
Liam Tiarnach
Ranch Hand

Joined: Aug 06, 2004
Posts: 51
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 ]

- Liam...<br />- ' He who never sleeps... '
James Chegwidden
Author
Ranch Hand

Joined: Oct 06, 2002
Posts: 201
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
Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
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
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
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.


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
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.
James Chegwidden
Author
Ranch Hand

Joined: Oct 06, 2002
Posts: 201
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
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
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.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Tom Hill
Ranch Hand

Joined: Aug 24, 2003
Posts: 115
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
Marcinovkovic Leonovic
Greenhorn

Joined: Aug 26, 2004
Posts: 1
>
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Evaluation of nested loops