• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why does this work?

 
Riley Redd
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 9462
49
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Riley Redd
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64178
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Riley Redd
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64178
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Pie
Posts: 9462
49
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic