Larger increments in For..Do Loops. lazarus / 21-General ---------------------------------------------------------------------- OC DelGuy · 2021-02-10 20:37 How can you make the For..Do loop cycle in higher increments? I remember in Basic you could put a "Step" command after the For command: Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i For x=2 to 10 Step 2.....Next X Is there a Pascal equivalent? ---------------------------------------------------------------------- balazsszekely · 2021-02-10 20:44 QuoteHow can you make the For..Do loop cycle in higher increments?Use while instead of for: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  x: Integer;begin  x := 2;  while x <= 10 do  begin    Writeln(x);    Inc(x, 2)  end;  Readln;end. ---------------------------------------------------------------------- MarkMLl · 2021-02-10 20:58 Use  2 * x  inside the loop. It's interesting to speculate on whether  step  (which uses an addition rather than an increment) or a multiplication (which can optimise to a shift) is more efficient. There's probably very little in it, but it might vary depending on whether every ALU in a core has a barrel shifter. MarkMLl ---------------------------------------------------------------------- marcov · 2021-02-10 21:10 If you really need this functionality a lot, you can mess around with custom iterators. This one only parametrizes upper limit, but you could parametrize lower,upper, increment etc. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$mode delphi}type TOddIterator = record    I: Integer;    flimit : INTEGER;public    function MoveNext: Boolean;    class function oddrecs(limit:integer):ToddIterator; static;    function getenumerator : ToddIterator;    property Current: Integer read i;  end; function TOddIterator.getenumerator : ToddIterator;beginresult:=self;end;class function TOddIterator.oddrecs(limit:integer):ToddIterator;   begin    result:=default(Todditerator);    result.i:=-1;    result.flimit:=limit;          end; function TOddIterator.MoveNext: Boolean;begin  inc(i,2); result:=i= first then        begin              setLength(step, (last - first) div delta + 1);                for i := low(step) to high(step) do                begin                      step[i] := first + i * delta;                end;        end      else        begin                step := nil; // empty list        end;end; var        x: int64;begin        for x in step(-4, 8, 2) do      begin                writeLn(x);        end;end.Just to demonstrate the principle. MarkMLl’s solution is still certainly the easiest, but requires you to manually calculate which i should be the for-loop’s final value, so i * 2 (or whatever the step-width may be) does indeed result in your actually desired final value. PS: Browsing the archive I didn’t know I actually started one of those “gimme different step-width” threads. I just want you to take a look at BeniBela’s solution, though. ---------------------------------------------------------------------- winni · 2021-02-10 22:52 If you insist  on the C syntax then there is an ugly solution: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i step :=3;for i := 0 to 1000 do  begin   if i mod step = 0 then       begin       .....       end;  end;  The Pascal people howl. The CPU weeps. But I have seen this before. It is better that you try to learn Pascal. So use while or an enumerator as shown before. Winni ---------------------------------------------------------------------- Bart · 2021-02-11 22:00 If you want to be really ugly them modify the loop counter via a pointer. IIRC then there is a bugreport about this... Bart ---------------------------------------------------------------------- 440bx · 2021-02-11 22:30 Instead of using a "for" statement, use a "while" statement. That allows stepping however the programmer wants including skipping steps if necessary. ---------------------------------------------------------------------- Bart · 2021-02-11 22:36 Quote from: Bart on February 11, 2021, 11:00:17 pmIIRC then there is a bugreport about this... Found it: Issue #25549. The idea has not been rejected... Bart ---------------------------------------------------------------------- Warfley · 2021-02-12 12:16 Quote from: MarkMLl on February 10, 2021, 09:58:43 pmUse  2 * x  inside the loop. This is what I always use, as I feel the alternatives are always much more cumbersome. While requires separate initialization and increment code and enumerators require a lot of boilerplate code (and also being usually a good bit slower compared to while or for loops). Also for loops are pretty neat in general as they take care of the edge cases (while x <= limit... Inc(x) will break if limit is the maxint value, because this results in an overflow). So I think this is the cleanest solution for iterating from lbound to ubound in steps of size step: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i for i_step := lbound to ubound + (lbound-ubound) div step dobegin  i := i_step * step;  // Continue with i as usualend; ---------------------------------------------------------------------- MarkMLl · 2021-02-12 14:19 Quote from: Warfley on February 12, 2021, 01:16:24 pmAlso for loops are pretty neat in general as they take care of the edge cases (while x <= limit... Inc(x) will break if limit is the maxint value, because this results in an overflow). That's a particularly good point. I don't know about anybody else though, but I sometimes feel that I ought to be using enumerations and subranges far more heavily so that I'm never explicitly declaring an array by its bounds. However I'm troubled by the behaviour if the enumeration contained something like Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Type    EnumType = (one, two, three, forty := 40,fortyone);  MarkMLl