• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting at the if block containing "d"

 
Jay Hale
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't mean to bump an old thread but, was trying to figure out how "d" gets assigned when I think about this code it shows up as 1 as the last result when doing x = x - 1 on the last if statement.. Does it then take the returned result "1" and go back to x = x - 1; System.out.print("-") which then results to 0 ? Thanks so much!!



When I run it it shows returned results of a-b c-d . I can't seem to understand how "-" and "d" -d got assigned.
 
Jay Hale
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now that I look at the last if statement more. The last if statement will not run if x == 1 and will not println nor perform x = x - 1 correct?
 
Vijitha Kumara
Bartender
Pie
Posts: 3918
10
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch, Jay Hale!

The last if statement will not run if x == 1 and will not println nor perform x = x - 1 correct?

Yes. But the while loop runs recursively until x > 0.

Hint: Think about the second run of the entire while loop and value of x. After the first run of the loop x==2, and what happens in the second run?

BTW, you may post your questions on a separate thread.

 
Campbell Ritchie
Sheriff
Pie
Posts: 50175
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vijitha Kumara wrote: . . . BTW, you may post your questions on a separate thread.
Agree. I can split this question in to a new thread; it appears to me to be a new question.
 
Tarun Bolla
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jay Hale...
Get a good IDE...like Eclipse and learn to trace a program. That helps you a lot...with these kind of questions
 
Campbell Ritchie
Sheriff
Pie
Posts: 50175
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think using Eclipse at this stage might be too steep a learning curve. You can use the debugger facility, but that takes no slight effort to learn.
 
Jay Hale
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tarun Bolla wrote:Hi Jay Hale...
Get a good IDE...like Eclipse and learn to trace a program. That helps you a lot...with these kind of questions
I'm using eclipse now any idea where to get started to perform a trace? Thanks!!
 
Campbell Ritchie
Sheriff
Pie
Posts: 50175
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Double-click the left margin of your code, and a little blue spot appears. That is a breakpoint. Use the debug mode rather than the run mode and it stops at the breakpoint. You can now step through the code with step into, step over and step return commands (I think on the f keys, f5 f6 and f7, too). You can pause at any point and inspect the values of all fields, and local variables too.

If you use NetBeans, there is a big big difference in the process: NetBeans breakpoints are red.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic