views:

168

answers:

6

Ok, I'm unsure if my return line will end the for() loop or just the if() question?

Example:

for(;;) {
  wait(1);
  if(something) {
    tokens = strTok(something, " ")
    if(tokens.size < 2)
      return;
   }
}

I'm guessing that it'll just return from the if(something) question but I just want to be sure...

+1  A: 

This may depend on the particular language but for all the languages I can think of return will return from the current function. FOR() and IF() structures don't usually have return statements.

+2  A: 

return in most languages will end the entire method.

ggf31416
+13  A: 

In C-like languages, return exits the entire function. break will exit the innermost loop (for do...while or while)

Jacob
+3  A: 

In all languages I know (except haskell) return will return from enclosing function/method, while break would "return" from the loop.

sepp2k
In XQuery and XPath 2.0, `return` is FLWOR keyword which is exactly equivalent to C# LINQ `select` - i.e. what you write as `from x in xs select x` in C#, you'd write as `for $x in $xs return $x` in XQuery.
Pavel Minaev
+1  A: 

RETURN is, for all languages I know, "Stop doing what you're doing and exit this function completely". From your description you apparently don't want RETURN, you want an BREAK or CONTINUE, depending on the language you're using.

DaveE
+1  A: 

This is presumably all inside a function or method; RETURN will exit that function/method.

To give an example of a more procedural setting, in a PHP file a RETURN that isn't in a function will exit the current script file. (Again, it won't matter if it's inside other blocks.)

JacobM