开发者

What does return do when used inside an if statement?

What does the return inside the if statements do in the following code?

public void startElement(String namespaceURI, String localName,String qName, 
                                         Attributes atts) throws SAXExc开发者_如何学Goeption
{
    depth++;
    if (localName.equals("channel"))
    {
        currentstate = 0;
        return;
    }
    if (localName.equals("image"))
    {
        // record our feed data - you temporarily stored it in the item :)
        _feed.setTitle(_item.getTitle());
        _feed.setPubDate(_item.getPubDate());
    }
    if (localName.equals("item"))
    {
        // create a new item
        _item = new RSSItem();
        return;
    }
    if (localName.equals("title"))
    {
        currentstate = RSS_TITLE;
        return;
    }
    if (localName.equals("description"))
    {
        currentstate = RSS_DESCRIPTION;
        return;
    }
    if (localName.equals("link"))
    {
        currentstate = RSS_LINK;
        return;
    }
    if (localName.equals("category"))
    {
        currentstate = RSS_CATEGORY;
        return;
    }
    if (localName.equals("pubDate"))
    {
        currentstate = RSS_PUBDATE;
        return;
    }
    // if you don't explicitly handle the element, make sure you don't wind 
           // up erroneously storing a newline or other bogus data into one of our 
           // existing elements
    currentstate = 0;
}

Does it takes us out of the if statement and proceeds to next statement or it takes us out of the method startElement?


The returns in the above code will take you out of the method.


It finishes the method so the code below it, is not executed.


Does it takes us out of the if statement and proceeds to next statement or it takes us out of the method startElement?

It takes you out of the method.. The return statement terminates the execution of a function


return always takes control out of calling method.


Yes. The return here will take the control out of method.


it will return what you declared in the method head (here void = nothing = it will just end the method)


The return here is probably used in order to "improve" the performance of the method, so that other comparisons are not executed, once the needed scenario is performed.

However, it's not good practice to have multiple return points in a method.

As stated in my comments I'd try a different approach to achieve the flow of the code in question.


The return will end the flow of the method, and is functionally identical to using a shorter else if chain like

/* if (localName.equals("channel")) {
    currentstate = 0; // This can be removed because it's the default below.
} else */ if (localName.equals("image")) {
    // record our feed data - you temporarily stored it in the item :)
    _feed.setTitle(_item.getTitle());
    _feed.setPubDate(_item.getPubDate());
} else if (localName.equals("item")) {
    // create a new item
    _item = new RSSItem();
} else if (localName.equals("title")) {
    currentstate = RSS_TITLE;
} else if (localName.equals("description")) {
    currentstate = RSS_DESCRIPTION;
} else if (localName.equals("link")) {
    currentstate = RSS_LINK;
} else  if (localName.equals("category")) {
    currentstate = RSS_CATEGORY;
} else if (localName.equals("pubDate")) {
    currentstate = RSS_PUBDATE;
} else {
    currentstate = 0;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜