Does Return Stop a Loop

Does return stop a loop?

Yes, function's always end when ever there control flow meets a return statement.

The following example demonstrates how return statements end a function's execution.

function returnMe() {
for (var i = 0; i < 2; i++) {
if (i === 1) return i;
}
}

console.log(returnMe());

Difference between Return and Break statements

break is used when you want to exit from the loop, while return is used to go back to the step where it was called or to stop further execution.

How can I use `return` to get back multiple values from a loop? Can I put them in a list?

Using a return inside of a loop will break it and exit the function even if the iteration is still not finished.

For example:

def num():
# Here there will be only one iteration
# For number == 1 => 1 % 2 = 1
# So, break the loop and return the number
for number in range(1, 10):
if number % 2:
return number
>>> num()
1

In some cases we need to break the loop if some conditions are met. However, in your current code, breaking the loop before finishing it is unintentional.

Instead of that, you can use a different approach:

Yielding your data

def show_todo():
# Create a generator
for key, value in cal.items():
yield value[0], key

You can call it like:

a = list(show_todo())  # or tuple(show_todo())

or you can iterate through it:

for v, k in show_todo(): ...

Putting your data into a list or other container

Append your data to a list, then return it after the end of your loop:

def show_todo():
my_list = []
for key, value in cal.items():
my_list.append((value[0], key))
return my_list

Or use a list comprehension:

def show_todo():
return [(value[0], key) for key, value in cal.items()]

does return 0 exit a program or exit a loop?

It will return from the main method, which in this case, will exit the whole program.

what is the difference between return and break in python?

break is used to end a loop prematurely while return is the keyword used to pass back a return value to the caller of the function. If it is used without an argument it simply ends the function and returns to where the code was executing previously.

There are situations where they can serve the same purpose but here are two examples to give you an idea of what they are used for

Using break

Iterating over a list of values and breaking when we've seen the number 3.

def loop3():
for a in range(0,10):
print a
if a == 3:
# We found a three, let's stop looping
break
print "Found 3!"

loop3()

will produce the following output

0
1
2
3
Found 3!

Using return

Here is an example of how return is used to return a value after the function has computed a value based on the incoming parameters:

def sum(a, b):
return a+b

s = sum(2, 3)
print s

Output:

5

Comparing the two

Now, in the first example, if there was nothing happening after the loop, we could just as well have used return and "jumped out" of the function immediately. Compare the output with the first example when we use return instead of break:

def loop3():
for a in range(0, 6):
print a
if a == 3:
# We found a three, let's end the function and "go back"
return

print "Found 3!"

loop3()

Output

0
1
2
3

Using return to exit a loop?

return will exit the current method (Main in your example). Use break to exit the loop.



Related Topics



Leave a reply



Submit