Forum > Beginners

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

<< < (2/22) > >>

molly:
@GetMem:
I just wanted to avoid the misconception that the c code you wrote can't have a Pascal counterpart (the c code seemed to me as placed out of context so i allowed myself to do so as well).

Ok, so ... the following is a for loop ? ;-)


--- Code: ---var
  i : integer;
  ia: array of integer;
begin
  i := 0;
  while i < 10 do
  begin
    SetLength(ia, Length(ia)+1);
    ia[High(ia)] := i;
    i := i + 2;
  end;
   
  for i in ia do WriteLn('i: ',i);
end;

--- End code ---

But, it does raise the question why not support something like:
for i := 0 to 10 step 2 do ...

There once was a time when it was allowed to assign another value to the iterator. A step modifier could at least lift some restriction.

balazsszekely:

--- Quote ---I just wanted to avoid the misconception that the c code you wrote can't have a Pascal counterpart
--- End quote ---
In my opinion there is no c code that cannot be done in pascal, perhaps not so efficiently but it can be done.
Regarding your for loop, it's a valid one, but you increased the complexity of the algorithm, now it's a double loop, looks nice though.  :)


--- Quote ---There once was a time when it was allowed to assign another value to the iterator. A step modifier could at least lift some restriction.
--- End quote ---
Yes, I agree. I don't think it's gonna be implemented though.

minesadorada:
Reminds me of Sinclair Spectrum BASIC

10 FOR n=10 TO 1 STEP -1
20 PRINT n
30 NEXT n

BeniBela:
How about


--- 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 in range(0, 10, 2) do    writeln(i); 
?

Needs a little help


--- 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 TIntRange = record  c: integer;  f,t,s: integer;  function getEnumerator: TIntRange;  function moveNext: boolean; inline;  property current: integer read c;end; function range(f,t: integer; s: integer=1): TIntRange;begin  result.c := f - s;  result.f := f;  result.t := t;  result.s := s;end; function TIntRange.getEnumerator: TIntRange;begin  result := self;end; function TIntRange.moveNext: boolean;begin  c += s;  result := c <= t;end; 

skalogryz:

--- Quote from: molly on December 15, 2016, 06:53:32 am ---nay sayer !  >:D ;-)

Of course you can do exactly that. You just have to write it a bit differently  :P

--- Code: ---var
  i: integer;
begin
  i := 0;
  while i < 10 do
  begin
    WriteLn('i: ', i);
    i := i + 2;
  end;
end;

--- End code ---

--- End quote ---

Not exactly.
Pascal evaluates the number of iterations prior to the execution of the for loop.  Thus the loop should/could/would look like this:

--- Code: ---var
  i: integer;
  c: integer;
begin
  i := 0;
  c:=10 div 2; // one time count evaluation
  while c>0 do // efficient check for the end of the loop
  begin
    WriteLn('i: ', i);
    i := i + 2;
    dec(c);
  end;
end;

--- End code ---
It looks like a nonsense in this case, but when it comes to deal with real-like loops (i.e. traversing an indexe-list or a string, etc), it all starts to make sense.

Speaking of syntax. Why to introduce a new keyword? Instead an existing colon-construction could be used. Such as this.

--- Code: ---for i:=1 to 10:2 do
  writeln

--- End code ---
colon-construction is used in language-special routines, such as: Str, Write, Writeln.. etc

And, introducing "step" might come in conflict with "downto".

But what algorithms require "step"? ... or is just for educational tasks?

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version