• Post Reply Bookmark Topic Watch Topic
  • New Topic

Understanding the FOR loop  RSS feed

 
Rodrigo Pinheiro
Greenhorn
Posts: 2
AngularJS Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

I`m new around. I hope this question is relevant for other beginners.
I`m trying to understand some code about graphs. Although the algorithm issue itself is simple.

So here`s is my method that search for a vertex class in an array list.

private Vertice searchVerticeRef(String item)
{
Vertice res = null;
int i;

for (i = 0; ((i < vert.size()) && !vert.get(i).getItem().equals(item)); i++);

if (i < vert.size())
res = vert.get(i);

return res;
}



So here`s my doubt: I don`t quite understand how the for loop works here. It just executes the for line once. When does it increments the i variable? Also the i variable is always being updated to zero every time the method is called? If so how does it maintain its previous value on a new call?
I hope this question is clear enough. Thanks in advance!


 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

The way that for-statement was written, is tricky. Especially because it has no body, just a ";" (semi-colon) at the end, which is equivalent to an empty body.

Translated to English, the statement means:

Do a loop, start with setting i to 0, and no nothing (which is the ";" at the end) as long as i is less than vert.size() and vert.get(i).getItem() is not equal to 'item'; increment i after every iteration.

The idea is to search for the index of a Vertice object for which the item is equal to the item that was passed to the method.

There are better, more readable ways to write this code. For example, with a while loop:


I would have written the whole method differently:

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Not sure what you are asking here... are you asking how the "for" loops works in general? Or are you asking how it works in this case?

Rodrigo Pinheiro wrote: When does it increments the i variable?


It is incremented by the "increment" (aka. "reinitialization" statement) of the loop.

Rodrigo Pinheiro wrote: Also the i variable is always being updated to zero every time the method is called? If so how does it maintain its previous value on a new call?


It doesn't maintain state between calls. The variable is initialized (to zero) in the "initialize" statement of the for loop.

Henry
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

When posting code, please UseCodeTags (← click that link to learn how)

The code you posted has a few bugs in it. First, the semi-colon at the end of the for-loop header actually terminates the for-loop, giving it an empty body. That means the if statement that follows is not part of the for loop. So this code will return either null or whatever vert.get(0) is. This is assuming of course that the code even compiles. I assume you have a vert field declared in the class this method belongs to.

Edit: Jesper is right, the formulation of your for-loop is tricky. It even fooled me into thinking that semicolon at the end was a bug.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again
There is bad design in a method which intentionally returns null; it would be better to return the index at which the vertex/vertice was found. Use −1 for not found, rather like String#indexOf().
Junilu Lacar wrote:The code you posted has a few bugs in it. First, the semi-colon at the end of the for-loop header actually terminates the for-loop, giving it an empty body.
The C people used to like loops like that, because they were short of memory. Now we have lots of memory, so nobody need write such confusing code. It shou‍ld be followed by a comment // empty block intended otherwise people finding the ; will try to “correct” it.
 
Rodrigo Pinheiro
Greenhorn
Posts: 2
AngularJS Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for the inputs guys. I get it now!
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done sorting it out
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:The C people used to like loops like that, because they were short of memory. Now we have lots of memory, so nobody need write such confusing code. It shou‍ld be followed by a comment // empty block intended otherwise people finding the ; will try to “correct” it.

When appropriate, I still find the terseness of it very elegant. I agree though, you have to put a comment to make it clear that the body is intentionally left empty. Any of these variants work for me:

Although if a for loop like that had more code around it, I'd extract it to its own method.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!