Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
1992
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 1992

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Loop problem

  • To: mathgroup at yoda.physics.unc.edu
  • Subject: Re: Loop problem
  • From: TODD GAYLEY <TGAYLEY at ccit.arizona.edu>
  • Date: 08 Oct 1992 10:54:16 -0700 (MST)

Stefano Pagiola (spagiola at fri-nxt-pagiola.stanford.edu) writes:
 
>  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]]
>          }]
>  
>   (...deleted...)
>  
>  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.
 
Your problem is not with Break or Return, but with the For loop itself.
The semicolon after t++ is incorrect; you need a comma here instead.
The semicolon causes the long "body" of the loop to instead be grouped with
the increment portion. Thus, there is in fact no body to the loop at all.
Break and Return statements are only effective when generated in the body of a
loop or function.
 
Also, the braces {} around the "body" code are a little odd. Braces are only
used to construct lists. Parentheses are used to group multiple statements
separated by semicolons, when it is necessary. In the corrected version of the
loop, parentheses will not be necessary (the commas provide all needed
grouping information).
 
--Todd
 
-----------------------------------------------------------------
Todd Gayley
Department of Ecology and Evolutionary Biology
University of Arizona
tgayley at ccit.arizona.edu       -or-        tgayley at cs.arizona.edu





  • Prev by Date: quaternions
  • Next by Date: Plotting interpolating functions
  • Previous by thread: Re: Loop problem
  • Next by thread: How to get a matrix multiply that considers dimensions?