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?
When a return statement is used in a function body, the execution of the function is stopped. If specified, a given value is returned to the function caller. For example, the following function returns the square of its argument, x , where x is a number. If the value is omitted, undefined is returned instead.
Is it possible to write return statement in if block? It is possible, but you haven't covered all the possible cases to which the program flow may go.
No, both values aren't going to be returned. A return statement stops the execution of the method right there, and returns its value. In fact, if there is code after a return that the compiler knows it won't reach because of the return , it will complain.
We can use the return statement inside a function only. In Python, every function returns something. If there are no return statements, then it returns None. If the return statement contains an expression, it's evaluated first and then the value is returned.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With