File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Cattle Drive and the fly likes Java 1b: For loop question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "Java 1b: For loop question" Watch "Java 1b: For loop question" New topic
Author

Java 1b: For loop question

Connie Boyd
Ranch Hand

Joined: Feb 22, 2001
Posts: 73
Hi! I got a nitpick to start for loops at zero whenever possible, and while I agree and normally do, I think in this case it seems to make the code more clugy.
When starting at one, I can perform a straight modulo operation on the index and another variable. If I start at zero, I have to add one to the index and then perform the modulo. So something like:
if ( ( i % myVariable ) == 0 )
becomes:
if ( ( ( i + 1 ) % myVariable ) == 0 )
Does this really make the code easier to read? Any thoughts would be greatly appreciated.
Bruce Wingate
Ranch Hand

Joined: Feb 16, 2001
Posts: 32
For consistency, and ease of reading the loops, I always start my loops at 0, unless I'm going backwards. Another rule I follow is to never modify the loop variable in a for loop. Technically you're not modifying the loop variable, but doing the modulo to it is close.
Maybe using a temp variable would clean things up?
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
The result of a mod doesn't always have to be equal to zero.
Bill
Connie Boyd
Ranch Hand

Joined: Feb 22, 2001
Posts: 73
Thanks for the input, but I'm still on the fence.
I'm using the modulo to see that the number of names written to the screen so far is evenly divisible by the max number of names allowed on that line. So, having a result of zero seems to be the ideal check here. I could code for a different modulo result, but it's still not an intuitive comparison.
Putting the index + 1 in a separate variable would definitely clean up the code, but to me it seems artifical. If I commented it for future developers, it would just say "//Add one to index for modulo operation." Why not add one to the index in defining the for loop?
Am I being hard-headed?
Anyway, this is helping me to think of other ways. I'm not ready to just change the index without questioning, but maybe I'm just not seeing something that's there.
Sigh. Back to the drawing boards.
[This message has been edited by Connie Boyd (edited March 07, 2001).]
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
Another way you could do this is to print the line first if needed and then print the name.
Bill
Connie Boyd
Ranch Hand

Joined: Feb 22, 2001
Posts: 73
That was it!! Thanks, Bill, no wonder you're a bartender. I was so married to printing out the names, then the new line, that I forgot you could also do it the other way.
Thanks for all the help. Hopefully I will be able to move on to the second assignment.
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
No problem. Just wait to the 4th assignment. I found that one to be the hardest of them so far.

Bill
Connie Boyd
Ranch Hand

Joined: Feb 22, 2001
Posts: 73
Believe me, I've heard. And I'm looking forward to it with giddy anticipation
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java 1b: For loop question
 
Similar Threads
Out of bounds Exception
convert string to an int
divide by zero: Not a compile time error
Array item selection
database access from JSP