aspose file tools*
The moose likes Beginning Java and the fly likes Why would the following for loop get skipped? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why would the following for loop get skipped?" Watch "Why would the following for loop get skipped?" New topic
Author

Why would the following for loop get skipped?

Jordan D. Williams
Ranch Hand

Joined: Jan 03, 2012
Posts: 51

Hello everyone.

I have been trying to work with ArrayList and I am having a bit of a problem. I need to make an ArrayList that will hold a variable number of elements depending on how many coefficients are needed in order to solve the equation. The number of elements depends on the "equationType" that is selected - this is just part of a bigger program that I am working on. I have a for loop that needs to collect the coefficients, which is supposed to use another ArrayList that holds the coeffLetters (the letters which correspond to the different coefficients). When I try to run the for loop to get the actual coefficients, it simply gets skipped (from line 30 to line 43 of the ArayListTest class) and I have no idea why. I tried isolating only the ArrayLists and for loops portion and running it independently of the rest of the program, but I still could not get it to work.



The Utils class is my adaptation from a utility class that was written by Campbel Ritchie on these forums. readIntFromIn was written by him. I tried to create the readIntFromDouble and there is a good chance that I did it wrong...


Any help would be appreciated!

Thanks!

[Reason for Edit: Specified which for loop gets skipped]

John 3:16
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4422
    
    8

Surely you meant to have i < coeffLetters.size()?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

I don't know which for loop you think is being skipped, but given for (initialization; condition; increment), if the loop's body is never being entered, then condition is false when we first hit that for statement.
Jordan D. Williams
Ranch Hand

Joined: Jan 03, 2012
Posts: 51

Matthew Brown wrote:Surely you meant to have i < coeffLetters.size()?


Yes, I should have had that!! How silly! I forgot that the size() function will return the total number of indexes, in this case 3, while the first index position is 0. Therefore I should have it as i < coeffLetters.size() since I only need 3 coefficients in this case. However, is it possible to write a for loop in the way that I had it? Or can in only take "less than" < ?

Jeff Verdegan wrote:I don't know which for loop you think is being skipped, but given for (initialization; condition; increment), if the loop's body is never being entered, then condition is false when we first hit that for statement.


Apologies Jeff. The for loop that gets skipped is the second one. The code execution jumps from line 30 to line 43 of the ArrayListTest class.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Jordan D. Williams wrote:However, is it possible to write a for loop in the way that I had it? Or can in only take "less than" < ?


Any boolean expression is legal there. That condition means "keep the loop going as long as thing condition is true. You can write <, <=, ==, != >, >=, a method call that returns boolean, the ternary operator, or any combination of the above with &&, ||, &, |, and ^. As long as it's a boolean expression, it's legal to put it there, and as long as it accurately describes your continuation condition, your code will work. Of course, just because you can do it, that doesn't mean you should.

And note that the 0..3 issue is not why it didn't work for you. It didn't work the way you had it because you said, "keep going as long as i is 3". Since i wasn't 3 to start with, you never entered the loop body.
Jordan D. Williams
Ranch Hand

Joined: Jan 03, 2012
Posts: 51

Jeff Verdegan wrote:
And note that the 0..3 issue is not why it didn't work for you. It didn't work the way you had it because you said, "keep going as long as i is 3". Since i wasn't 3 to start with, you never entered the loop body.


If you were here here when I read that you would have seen the room illuminate as the light bulb shone brightly inside my head! I was totally confused on that one. I thought the loop will keep going until i equals 3. I should have known this though because if that was the case, then my loop at line 19 would have never executed, since in the very beginning [tt]c[/t] is less than coeffsNeeded.

Just when I thought I had the for loop down, it threw a curve ball at me.

Thanks so much for the explanation. It makes perfect sense now.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

You're very welcome! I'm glad it's cleared up.
 
Consider Paul's rocket mass heater.
 
subject: Why would the following for loop get skipped?