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 Why does this work? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why does this work?" Watch "Why does this work?" New topic
Author

Why does this work?

Riley Redd
Greenhorn

Joined: Jul 28, 2013
Posts: 9
I am currently going through the book Absolute Java, 5/E and when I reached this problem I was stumped. I couldn't figure out to do this, so I looked online and found this (Yes, Riley is a bad lazy boy). This program works, but I don't understand why.

My question is this:
How do the 'for' statements know to what is the correct number to stop on for the statement to be correct? Is the 'if' statement directly below controlling it?

edit: I figured I might as well state that I just started learning Java (and programming) about 2 weeks ago. This is not an excuse for blatantly plagerizing someone else's code.

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42949
    
  70
Without knowing what this code is supposed to be doing it's hard to comment on whether what it's doing is correct.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

Riley Redd wrote:I am currently going through the book Absolute Java, 5/E and when I reached this problem...

Erm....what problem? I'm not familiar with the book, and you've plonked a bunch of code in front of us (which I've broken up a bit; please DontWriteLongLines).

I have no problem reverse-engineering stuff, but I'm usually paid big bucks to do it; and I'm also usually given at least a hint as to what it's supposed to be doing.

So - you want me to work for free? You'll need to provide a LOT more information than you have (lazy Riley?).

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Riley Redd
Greenhorn

Joined: Jul 28, 2013
Posts: 9
WOAH!!! I don't want any reverse engineering!!! I just want to know how 'for' works like it does in this program!!!

I assumed something like this would just do a permanent loop, yet it doesn't.

Edit: The program works like it is supposed to, I am just trying to understand the usage of the 'for' loop so that I may use it better in the future.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61769
    
  67

The second clause of the for statement sets the termination. For example, letterT < 10 loops while the value of letterT is less than 10 (but not equal to 10, note carefully).

Of course, if the code ever hits System.exit() everything comes to a crashing halt immediately.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Riley Redd
Greenhorn

Joined: Jul 28, 2013
Posts: 9
Bear Bibeault wrote:The second clause of the for statement sets the termination. For example, letterT < 10 loops while the value of letterT is less than 10 (but not equal to 10, note carefully).

Of course, if the code ever hits System.exit() everything comes to a crashing halt immediately.


I have to be completely honest...the bolded section went completely over my head.

edit: I don't even know what the first clause is ^.^'
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61769
    
  67


1st clause: letterT = 0 initializes the iterator
2nd clause: letterT < 10 sets the condition for loop continuance (the loop will continue while the condition is true)
3rd clause: letterT++ updates iterator on every iteration

See: The for Statement
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

Riley Redd wrote:like it does in this program

Er...is there there something that wasn't clear about "please DontWriteLongLines"?

It's just that I notice that you've now edited your post a further 9 times, lengthening
virtually every line, to make the thread just as unreadable now as it was to start with.

Winston
Riley Redd
Greenhorn

Joined: Jul 28, 2013
Posts: 9
@Bear Bibeault: Thank you! (I didn't know that:
for (initialization; termination; increment)
these were clauses. Good to know!

Also @Bear Bibeault: (in reference to editing posts) Sorry, I will never do it again, I promise.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why does this work?