uniquesupri
uniquesupri

Reputation: 289

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 SAXException
{
    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?

Upvotes: 28

Views: 87703

Answers (8)

Elliott Frisch
Elliott Frisch

Reputation: 201429

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;
}

Upvotes: 0

Alex
Alex

Reputation: 51

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

Upvotes: 3

Romeo
Romeo

Reputation: 337

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.

Upvotes: 0

eMi
eMi

Reputation: 5618

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

Upvotes: 15

Tushar Vengurlekar
Tushar Vengurlekar

Reputation: 7679

return always takes control out of calling method.

Upvotes: 4

Neifen
Neifen

Reputation: 2595

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

Upvotes: 2

mthpvg
mthpvg

Reputation: 3809

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

Upvotes: 16

Scott
Scott

Reputation: 1313

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

Upvotes: 24

Related Questions