Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does return do when used inside an if statement?

Tags:

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?

like image 924
uniquesupri Avatar asked Sep 12 '11 10:09

uniquesupri


People also ask

Can you use a return statement in an if statement?

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.

Can we write return statement in if block?

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.

Can we return in if?

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.

Can you return inside if statement Python?

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.


2 Answers

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

like image 130
Scott Avatar answered Oct 16 '22 01:10

Scott


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

like image 45
mthpvg Avatar answered Oct 16 '22 03:10

mthpvg