Loop problem
- To: mathgroup at yoda.physics.unc.edu
- Subject: Loop problem
- From: spagiola at fri-nxt-pagiola.stanford.edu (Stefano Pagiola)
- Date: Wed, 7 Oct 92 16:31:15 PDT
Dear Mathgroup,
I'm having no luck getting either Break[] or Return[] to get me out
of a loop. I have an iterating procedure in which values at each
step are computed from values at the previous step. If the new
values ever fail one of several tests, I want a message printed and
the loop to terminate. So I embedded several If[test,Break[]]
statements in my loop. When one of the tests fails, however, the
message prints but the loop does not terminate; rather, it seems to
go to the next step (ie, the behavior I expect of Continue[]).
Any ideas/suggestions?
Here's the code:
s[1] = 1;
l[1] = 1;
For[t = 0,t < n,t++;
{
a[t] = f[s[t],l[t]];
If[g[s[t],l[t]] <= 0,Return[Print["g[] < 0"]]];
l[t+1] = h[a[t],s[t],l[t]];
If[l[t+1] <= 0.05,Return[Print["l <= 0.05"]]];
If[l[t+1] > 25,Return[Print["l > 25"]]];
sss[t+1] = j[a[t],s[t],l[t]];
Print[t," s = ",s[t]," lambda = ",l[t]]
}]
And here's some sample output (skipping the early iterations):
...
88 s = 0.65357 lambda = 2.2681
89 s = 0.64986 lambda = 2.0907
90 s = 0.64609 lambda = 1.8881
91 s = 0.64226 lambda = 1.6572
92 s = 0.63834 lambda = 1.395
93 s = 0.63431 lambda = 1.098
94 s = 0.63012 lambda = 0.76208
l <= 0.05
l <= 0.05
l <= 0.05
l <= 0.05
l <= 0.05
...
As you can see, the l[t] < 0.05 test eventually fails, but the loop
does not terminate: it keeps right on going, and of course, the test
keeps failing. Break[] has the same results.
Anyy assistance would be greatly appreciated.
---
-
Stefano Pagiola
Food Research Institute, Stanford University
spagiola at frinext.stanford.edu (NeXTMail encouraged)
spagiola at FRI-nxt-Pagiola.stanford.edu (NeXTMail encouraged)