Jump to content

Quick Java help


aeonsky

Recommended Posts

private Node test(String symbol)
    {

        Node retval = null;
        Node node1 = portfolio;
        do
        {
            if(node1.next == null)
                break;
            if(((Stock)node1.item).getSymbol().equals(symbol))
            {
                retval = node1;
                break;
            }
            node1 = node1.next;
        } while(true);
        return retval;
    }

 

I was studying some code and had a question which I couldn't find the answer to.

 

This is the line...

 

while(true);

 

While what is true is it executing the code? Thank you!

Link to comment
Share on other sites

The do...while loop in the above code is an infinite loop - it will execute indefinitely, unless it is explicitly broken out of with a break statement. So yes, that loop will run until either of the two if statements evaluate to true.

 

I've never done any java, but i would guess that the function searches some sort of list structure.

 

 

Link to comment
Share on other sites

Well, since all that a while does is check if a condition evaluates to boolean true and then executes a block if it does, putting true as the condition is guaranteed to run the loop infinitely unless explicitly broken out of.  while(1) would do the same thing.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.