LOOP: Limit number of iterations when IF clauses present.
authorBen Pfaff <blp@cs.stanford.edu>
Sun, 10 Nov 2019 18:29:17 +0000 (18:29 +0000)
committerBen Pfaff <blp@cs.stanford.edu>
Sun, 10 Nov 2019 18:54:28 +0000 (18:54 +0000)
The LOOP command is limited by the MXLOOPS setting in some cirumstances.
Until now, if an IF clause was present, MXLOOPS was disregarded.
However, this behavior does not match SPSS behavior.  This commit makes
LOOP honor MXLOOPS even when IF is present, for compatibility.

With this commit, only the presence of an index clause makes LOOP
disregard MXLOOPS.

Thanks to Frans Houweling for reporting this bug.

doc/flow-control.texi
src/language/control/loop.c
tests/language/control/loop.at

index 04fd6c3c8fba4a1837c98deaeaac03078da766a4..9f4d41345760ae704f5afb0abd177ebeddabb42b 100644 (file)
@@ -182,8 +182,10 @@ code block is executed.  The condition is evaluated at the end of the
 loop, not at the beginning, so that the body of a loop with only a
 condition on @cmd{END LOOP} will always execute at least once.
 
-If neither the index clause nor either condition clause is
-present, then the loop is executed @var{max_loops} (@pxref{SET}) times.
+If the index clause is not
+present, then the loop is executed at most @var{max_loops} (@pxref{SET}) times
+(but possibly fewer, if a condition clause evaluates to false or if
+@cmd{BREAK} executes).
 The default value of @var{max_loops} is 40.
 
 @cmd{BREAK} also terminates @cmd{LOOP} execution (@pxref{BREAK}).
index 10d57105ccc8189d582be05dd501e04ae60a4a38..bfd364ccd1085a8d5874c6b018cfd2ded31fc4f8 100644 (file)
@@ -177,10 +177,7 @@ close_loop (void *loop_)
 
   /* If there's nothing else limiting the number of loops, use
      MXLOOPS as a limit. */
-  if (loop->max_pass_count == -1
-      && loop->index_var == NULL
-      && loop->loop_condition == NULL
-      && loop->end_loop_condition == NULL)
+  if (loop->max_pass_count == -1 && loop->index_var == NULL)
     loop->max_pass_count = settings_get_mxloops ();
 }
 
index d506a4d37f0522462d8a2a57ecc716509987888c..babab1c825f6cdf334237b75dcef4d69c6c10d84 100644 (file)
@@ -229,3 +229,34 @@ AT_CHECK([cat pspp.csv], [0], [dnl
 ])
 AT_CLEANUP
 
+AT_SETUP([LOOP with IF condition that ends due to MXLOOPS])
+AT_DATA([loop.sps], [dnl
+LOOP_DATA
+set mxloops=3.
+compute #p = x.
+loop.
+print /x #p.
+compute #p = #p + 1.
+end loop if #p >= 6.
+print/'--------'.
+execute.
+])
+AT_CHECK([pspp -o pspp.csv loop.sps])
+AT_CHECK([cat pspp.csv], [0], [dnl
+1     1.00 @&t@
+1     2.00 @&t@
+1     3.00 @&t@
+--------
+2     2.00 @&t@
+2     3.00 @&t@
+2     4.00 @&t@
+--------
+3     3.00 @&t@
+3     4.00 @&t@
+3     5.00 @&t@
+--------
+4     4.00 @&t@
+4     5.00 @&t@
+--------
+])
+AT_CLEANUP