-1

I am using the following code:

boolean continueProcessing = true;  
boolean lastRecord = false;     
while (continueProcessing)  //can it be changed to while (continueProcessing == true), what's the advantage  

{  
if(refListItemItr.hasNext() || lastRecord)  
{  
continueProcessing = true;  
lastRecord = false;  
}  
else  
{  
continueProcessing = false;  
}  

if(continueProcessing)  
{  
lSynonymListType = objFactory.createSynonymListType();  
}  
}  

I suspect there is a scenario of infinite loop. How should I confirm that it does not happen.

dev
  • 132
  • 1
  • 3
  • 11
  • Please format your code. – Paul R Jul 26 '12 at 06:46
  • This code doesn't appear to actually do anything... What are you trying to achieve here? Also, what is the type of refListItemItr? – Braiba Jul 26 '12 at 06:53
  • I have not written the whole code actually. It's quite big. Just wanted to know if thscenario can altogether be eliminiated? – dev Jul 26 '12 at 07:15

1 Answers1

2

The reason this will cause an infinite loop is that you keep checking if the next item exists, but you never actually call next() to retrieve it, so the internal pointer doesn't move along and you're just checking if there's a first item in the list.

Regardless, you're overcomplicating this. You should just be doing

while (refListItemItr.hasNext()){
  Object item = refListItemItr.next(); // change Object to the item's type
  // Presumably actually do something with the item here, which currently you're not...
}

Or, even more simply

for (Object o : refListItemItr){
  // do stuff
}

And in answer to your other question, there is absolutely no difference between while(continueProcessing) and while (continueProcessing == true)

Braiba
  • 571
  • 3
  • 8