1
$\begingroup$

Here is simplified version of my code that exhibits the behavior troubling me:

testFn[] := ( Do[ Print[i]; If[i > 3, Print["triggering result for true"]; Return [100]] , {i, 1, 5}]; Return[200]; ) testFn[] 1 2 3 4 triggering result for true 200 

Intuitively, once i is bigger than 3, it should evaluate the 2nd argument of the If and exit testFn, returning 100. However, although the 2nd argument of the If statement is getting evaluated, the function does not exit until it hits the Return[200].

I kind of guessed what might be the problem. The Return[100] is just exiting the Do, which seems to be confirmed by doing:

testFn[] := ( a = Do[ Print[i]; If[i > 3, Print["triggering result for true"]; Return [100]] , {i, 1, 5}]; Print["a=" <> ToString[a]]; Return[200]; ) 1 2 3 4 a=100 triggering result for true 200 

If this is indeed what's happening, how can I make my code actually return from and exit testFn?

$\endgroup$
2

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.