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;
for (i = 0; ((i < vert.size()) && !vert.get(i).getItem().equals(item)); i++);
if (i < vert.size())
res = vert.get(i);
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!
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:
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.
When posting code, please UseCodeTags (← click that link to learn how)
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.
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().
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 should be followed by a comment // empty block intended otherwise people finding the ; will try to “correct” it.
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.
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 should 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.