Why did this code only print the first element and not the rest?

Shouldn’t it had returned all the numbers that were divisible by 10? Which would have been 20, 30, and 40 but instead it printed out 20 and stopped. Why is that?

@java2115950072, don’t forget what return does:

  1. it returns a value to the calling statement
  2. It halts processing of the function.

Once return is reached one time, the function halts.


In addition to what @patrickd314 says, to obtain a list based on a list, you generally want to create an entirely new list rather than modifying the input. Why? It’s simpler, it leaves the original intact for future use, and it doesn’t really cost you anything.
You especially wouldn’t want to move values around (which you do by deleting elements) while you’re iterating through it, because that interferes with the iteration (you’re kind of pulling the rug out from under the loop)

1 Like

Thank you for this. Will keep it in mind next time