Forum > Beginners

for loop with custom step width (esp. ≠ 1)

<< < (22/22)

MarkMLl:

--- Quote from: munair on March 09, 2022, 08:45:45 pm ---A few days ago, when I looked at the poll, true and false were pretty even. Now false has gained quite a lead. I guess that means no stepping in the foreseeable future?

--- End quote ---

If you want it then see https://forum.lazarus.freepascal.org/index.php/topic,35141.msg427768.html#msg427768

MarkMLl

BubikolRamios:

--- Quote from: Kays on December 15, 2016, 02:52:05 am ---Hey,
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---for i := 0 step 2 until 10 dobegin  writeln(i);end;How do I make a for-loop iterate with a custom step width? So everything regarding iteration is in the top line. That'd be more readable than the corresponding Ersatz-statement. Thx for your answers!

--- End quote ---

just bumbed into that,  I think nobody invented this reverse logic (-:
step = 3 here


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---for i := 0 to x do begin      if i mod 3 <> 0 then Continue;      //do stuff ... end; 

Dzandaa:
Hi,

also:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---i := 0;while i < 20 dobegin // Do something inc(i, 2);end;    
B->

Warfley:
If anything the syntax should be:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---for i:=0 to 10 in steps of 2 do
But putting that aside, that is a very specific thing, but we already have the for ... in ... loop, for which you can do the following:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---{$ModeSwitch advancedrecords} type  TStepIterator = record    Start, Stop, Step: Integer;    property Current: Integer read Start;    function MoveNext: Boolean; inline;    function GetEnumerator: TStepIterator; inline;  end; function TStepIterator.MoveNext: Boolean;begin  Inc(Start, Step);  Result := Start <= Stop;end; function TStepIterator.GetEnumerator: TStepIterator;begin  Result := Self;end; function Steps(Start, Stop, Step: Integer): TStepIterator; inline;begin  Result.Start := Start - Step;  Result.Stop := Stop;  Result.Step := Step;end; var  i: Integer;begin  for i in Steps(0, 10, 2) do    WriteLn(i);end.
So there is absolutely no need for a dedicated syntax element for this. That said, what could be very useful is if ranges were dataobjects. So for example the 0..10 would not just be a type, but also a value that can be used for sets (if x in 0..10), for arrays (arr := [0..10]) or as lazy generator and enumerator (for x in 0..10). Maybe also with steps 0..10:2 to only have the even numbers, etc.
This would enable a lot more very cool features especially with custom enumerators.

Navigation

[0] Message Index

[*] Previous page

Go to full version