Forum > Beginners

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

(1/22) > >>

Kays:
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!

Handoko:
Maybe not the best solution, but I use this:

--- 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";}};} ---var  i, idx: integer;// ...for i := 0 to 5 dobegin  idx := i * 2;  writeln(idx);end;

balazsszekely:

--- 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; i <= 10; i = i + 2 ){  printf("i: %d\n", i);} :P I'm afraid you cannot do this in pascal.

molly:

--- Quote from: GetMem on December 15, 2016, 06:26:38 am ---I'm afraid you cannot do this in pascal.

--- End quote ---
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 ---

balazsszekely:
@molly
The OP explicitly talks about a for-loop and "everything regarding iteration is in the top line". Of course you can do it with a while/repeat loop. It can be done even with a recursive function.  :D