aspose file tools*
The moose likes Beginning Java and the fly likes Getting at the if block containing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Getting at the if block containing "d"" Watch "Getting at the if block containing "d"" New topic
Author

Getting at the if block containing "d"

Jay Hale
Greenhorn

Joined: Jul 28, 2011
Posts: 4
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

Joined: Jul 28, 2011
Posts: 4
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

Joined: Mar 24, 2008
Posts: 3837

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.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39380
    
  28
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

Joined: Jun 20, 2011
Posts: 89
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

Joined: Oct 13, 2005
Posts: 39380
    
  28
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

Joined: Jul 28, 2011
Posts: 4
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

Joined: Oct 13, 2005
Posts: 39380
    
  28
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting at the if block containing "d"