Group Abstract Group Abstract

Message Boards Message Boards

0
|
3.4K Views
|
2 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Why isn't Return giving me output from Do?

Posted 2 years ago
POSTED BY: Lewis Jones
2 Replies

ListAnimate[y2] makes me think you expect Do to be able to work like Table and produce a list of results of each iteration. It cannot. Return could return a single result the first time it is executed (a.k.a. "evaluated") in the Do loop. For it to return a plot, the argument to Return should be a plot. But the argument is not a plot in any of your examples.

Further, you place Return[..] as an argument to Plot[], in a position where Plot[] expects an option. I get error messages, which seem to be ignored in question.

If you want to make a list of output (plots or whatever), use Table or Map. They're the best ways. If you want to make Do[] construct your list then try something like this:

resultList = {}; (* initialize *)
Do[ nextResult = ...; AppendTo[resultList, nextResult], {...}]

Or this:

resultLL = {}; (* initialize linked list *)
Do[ nextResult = ...; resultLL = {resultLL, nextResult}, {...}];
resultList = Flatten[resultLL]; (* does not work if nextResult is a List *)

The second, linked-list way is usually more efficient than AppendTo.

POSTED BY: Michael Rogers

At that last position Do only allows the {i,a,b} range list

Return[] is leaving the loop somwher in the middle by any function that yields as a result a Return[---] in the compound statement, separated by semicolons on the level of the first argument of

Do[ a:  b:  v;....,  dom ]  

Do[  Print[i]  ;   If[i == 3, Return[   Print["Exit at ", i]]  ]  , {i, 1, 10}]
POSTED BY: Roland Franzius
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard