Difference between revisions of "Continue"

From Free Pascal wiki
Jump to navigationJump to search
(refactor, emphasize that the condition is reevaluated, mention MacPas cycle)
Line 1: Line 1:
 
{{Continue}}
 
{{Continue}}
  
The [[modifier|modifier]] <syntaxhighlight lang="pascal" enclose="none">continue</syntaxhighlight> belongs to the loop commands.
+
The <syntaxhighlight lang="pascal" inline>continue</syntaxhighlight> (pseudo) [[Routine|routine]] effectively skips the remainder of the [[Loops|loop]] body for one iteration, thus jumping back (or forward) to the loop head.
The <syntaxhighlight lang="pascal" enclose="none">continue</syntaxhighlight> [[statement|statement]] moves the loop immediately to the next iteration of the loop.
+
It is a non-standardized extension.
It can be used in any of the loop control structures (eg.
 
[[While|<syntaxhighlight lang="pascal" enclose="none">while</syntaxhighlight>]]..[[Do|<syntaxhighlight lang="pascal" enclose="none">do</syntaxhighlight>]],  
 
[[Repeat|<syntaxhighlight lang="pascal" enclose="none">repeat</syntaxhighlight>]]..[[Until|<syntaxhighlight lang="pascal" enclose="none">until</syntaxhighlight>]], and
 
[[For|<syntaxhighlight lang="pascal" enclose="none">for</syntaxhighlight>]]).
 
  
 +
<syntaxhighlight lang="pascal" inline>Continue</syntaxhighlight>, with its special meaning of terminating one iteration, can only be written ''within'' loops.
 +
It is not a reserved word, therefore you ''could'' use it as an [[Identifier|identifier]], but still access the special routine by writing its ''fully qualified'' identifier <syntaxhighlight lang="pascal" inline>system.continue</syntaxhighlight>.
 +
In [[Mode MacPas|<syntaxhighlight lang="delphi" inline>{$mode MacPas}</syntaxhighlight>]] the <syntaxhighlight lang="pascal" inline>cycle</syntaxhighlight> statement is an alias for <syntaxhighlight lang="pascal" inline>continue</syntaxhighlight>.
  
<syntaxhighlight lang="pascal">
+
== application ==
program ContinueProject;
+
The use of <syntaxhighlight lang="pascal" inline>continue</syntaxhighlight> is usually disfavored since it “delegitimizes” the loop’s condition expression.
uses sysUtils;
+
If the number of iterations can be predicted ''before'' the loop starts, it is even considered bad style to use <syntaxhighlight lang="pascal" inline>continue</syntaxhighlight>.
Const
+
Instead it is highly recommended to improve the algorithm.
  arr :array[1..10] of integer=(10, 9, 8, 7, 6, 5, 4, 3, 2, 1);
+
Nevertheless, in the case of ''unpredictable'' data, and thus an ''unpredictable'' number of (complete) iterations, <syntaxhighlight lang="pascal" inline>continue</syntaxhighlight> is generally acceptable.
 +
<syntaxhighlight lang="pascal" highlight="17">
 +
program continueDemo(input, output, stdErr);
 
var
 
var
   i, sum :integer;
+
   i, sum: integer;
  s:string;
 
 
begin
 
begin
 
   sum := 0;
 
   sum := 0;
   s := '';
+
   writeLn('Enter some positive numbers, line by line:');
   for i in arr do
+
    
     begin
+
  //      ┌─────────────────────────────────┐
       if odd(arr[i]) then continue;
+
  //      ↓ next iteration: check condition │
       if s<>'' then s := s + ' + ';
+
  while not eof do                       // │
       sum := sum + arr[i];
+
     begin                                 // │
       s :=  s + IntToStr(arr[i]);
+
       readLn(i);                         // │
 +
                                          // │
 +
       if i < 1 then                       // │
 +
        begin                            // │
 +
          writeLn('Hey! Stay positive!'); // │
 +
          continue; // → ────────────────────┘
 +
        end;
 +
     
 +
      {$push}
 +
      {$rangeChecks off}
 +
       sum := sum + i;
 +
       {$pop}
 +
      if sum < 0 then
 +
        begin
 +
          writeLn('I can’t handle that much positivity.');
 +
          halt(1);
 +
        end;
 
     end;
 
     end;
   writeln;
+
    
   writeln (s);
+
   writeLn('The sum of all your positive numbers is ', sum, '.');
  writeln ('Sum of even numbers: ', sum);
+
   writeLn('I like that positivity.');
   readln;
+
end.
end.  
 
 
</syntaxhighlight>
 
</syntaxhighlight>
 
+
After and at <syntaxhighlight lang="pascal" inline>continue</syntaxhighlight> (or <syntaxhighlight lang="pascal" inline>cycle</syntaxhighlight> in <syntaxhighlight lang="delphi" inline>{$mode MacPas}</syntaxhighlight>) the program jumps back, or in the case of [[Repeat|<syntaxhighlight lang="pascal" inline>repeat … until</syntaxhighlight>]] forward, to the loop’s condition.
 +
The respective condition must be satisfied again before another iteration occurs.
  
 
== see also ==
 
== see also ==
* {{Doc|package=RTL|unit=system|identifier=continue|text=<syntaxhighlight lang="pascal" enclose="none">Continue</syntaxhighlight>}}
+
* {{Doc|package=RTL|unit=system|identifier=continue|text=<syntaxhighlight lang="pascal" inline>continue</syntaxhighlight>}}
* [[Break|<syntaxhighlight lang="pascal" enclose="none">Break</syntaxhighlight>]]
+
* [[Break|<syntaxhighlight lang="pascal" inline>break</syntaxhighlight>]]

Revision as of 17:49, 13 July 2021

Deutsch (de) English (en)

The continue (pseudo) routine effectively skips the remainder of the loop body for one iteration, thus jumping back (or forward) to the loop head. It is a non-standardized extension.

Continue, with its special meaning of terminating one iteration, can only be written within loops. It is not a reserved word, therefore you could use it as an identifier, but still access the special routine by writing its fully qualified identifier system.continue. In {$mode MacPas} the cycle statement is an alias for continue.

application

The use of continue is usually disfavored since it “delegitimizes” the loop’s condition expression. If the number of iterations can be predicted before the loop starts, it is even considered bad style to use continue. Instead it is highly recommended to improve the algorithm. Nevertheless, in the case of unpredictable data, and thus an unpredictable number of (complete) iterations, continue is generally acceptable.

program continueDemo(input, output, stdErr);
var
  i, sum: integer;
begin
  sum := 0;
  writeLn('Enter some positive numbers, line by line:');
  
  //       ┌─────────────────────────────────┐
  //       ↓ next iteration: check condition │
  while not eof do                        // │
    begin                                 // │
      readLn(i);                          // │
                                          // │
      if i < 1 then                       // │
        begin                             // │
          writeLn('Hey! Stay positive!'); // │
          continue; // → ────────────────────┘
        end;
      
      {$push}
      {$rangeChecks off}
      sum := sum + i;
      {$pop}
      if sum < 0 then
        begin
          writeLn('I can’t handle that much positivity.');
          halt(1);
        end;
    end;
  
  writeLn('The sum of all your positive numbers is ', sum, '.');
  writeLn('I like that positivity.');
end.

After and at continue (or cycle in {$mode MacPas}) the program jumps back, or in the case of repeat  until forward, to the loop’s condition. The respective condition must be satisfied again before another iteration occurs.

see also