Win a copy of Functional Reactive Programming this week in the Other Languages forum!

# Trace this code

D. Jones
Greenhorn
Posts: 3

It's a sample exam problem and I'm having problems following what it's doing. I know when I enter a "-2" or a "0" it prompts me to enter another value and when I enter "3" it returns a "3". But I can't figure out why. It'd be great if you could take "-2" and "3" and walk me through what is performed.

Michael Novello
Greenhorn
Posts: 9
Do you understand while loops? Saying 'while ( n < i || n > j )' means 'while n is less than i OR n is greater than j'. The while loop will keep looping until both of these conditions are false (until 'n' is between 1 and 3). Every time it loops, the next user input, 'n' in this case, is again compared to 'i' and 'j'.

It's hard to tell exactly what you're asking, because it depends on how much you already know. If you know nothing, then you'll have to do some homework on your own, because for example, there is already a LOT to explain to someone who doesn't even know what a variable is.

D. Jones
Greenhorn
Posts: 3
I know what a variable is. And I pretty much know how while loops work. I guess I'm looking for a walk through of what happens when "-2" and "3" are entered and how they interact within the private and public method.

Ranch Hand
Posts: 105
My suggestion is. Take a pencil and paper, create a table that have a columns consist of Steps, n, i, j, loop condition check, result. Then for each step write the value of these variable (n, i, j, loop condition check, result). By doing this you can see how the variable changes in step-by-step, until you get the result.

D. Jones
Greenhorn
Posts: 3
Right. That's what I'm trying to do, but I'm not getting the right answer. I'm doing something fundamentally wrong. That's why I was hoping someone could kindly walk me through for two values (-2 & 3)

Jeff Verdegan
Bartender
Posts: 6109
6
D. Jones wrote:Right. That's what I'm trying to do, but I'm not getting the right answer. I'm doing something fundamentally wrong. That's why I was hoping someone could kindly walk me through for two values (-2 & 3)

Why don't you post what you think is happening, and then somebody here can give you a hint as to where your mistake lies.

Or add a bunch of println() statements that correspond to what you're tracking in your manual walk-through, and see where the printout of what's actually happening differs from your predictions.

James X Peterson
Whizlabs Java Support
Ranch Hand
Posts: 158
Hi D. Jones,