File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes How to manually track a variables value during loops Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to manually track a variables value during loops" Watch "How to manually track a variables value during loops" New topic
Author

How to manually track a variables value during loops

ray hancock
Greenhorn

Joined: Dec 31, 2012
Posts: 4
Is there a special technique for keeping track of a variables value while its in a loop. For example, I frequently run into pop quiz questions like "what is the value of x" and then after a loop or two the value of x only gets mentally lost in translation for me.

So.. this an easy basic loop

for (int i = 1; i <= 10; i++) {
System.out.println(i);

So we all know the output would be something like this.. Really easy to follow along.
1
2
etc..

But what about those loops that are just brain teasers when It comes to manually keeping track of what a variable's value is, there has to be some kind of trick or technique for making this easier to keep track of. I'm starting college soon and I'm sure I'll run into similar quiz questions. Thanks so much you guys are amazing.


Marjorie Gyles
Ranch Hand

Joined: Nov 03, 2012
Posts: 42

I find simple pencil and paper handy for keeping track of them.
Rajdeep Biswas
Ranch Hand

Joined: Mar 26, 2012
Posts: 186

Normally, on pen and paper as Lilith insisted. You can keep on striking off previous values as you write down the newer ones.


The biggest gamble will be to ask a question whose answer you know in that it will challenge your theory | www.TechAspire.blogspot.in
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7815
    
  21

ray hancock wrote:But what about those loops that are just brain teasers when It comes to manually keeping track of what a variable's value is, there has to be some kind of trick or technique for making this easier to keep track of.

I think it would be useful if you could provide an example of a "brain-teaser" loop, because I'm still not quite sure what you're asking.

However, in general, there are a few things that you can do to make it easier to extract the sort of information you want to print out:

1. Write dumb code (←click) - that is to say: don't try to be too clever.

2. Don't write statements with compound actions. So:
Don't write:
element[i++] = someValue;
Instead write:
element[i] = someValue;
++i;


3. (similar to above) Don't use "unnamed" values for critical things, ie:
Don't write:
doSomethingWith(generateAValue(x, y, z));
Instead write:
Value v = generateAValue(x, y, z);
doSomethingWith(v);


The above just scratches the surface, but hopefully gives you a few thing to think about.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to manually track a variables value during loops