Forum > Beginners

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

<< < (3/22) > >>

molly:
@BeniBela
Using a enumerator, nice !

I hadn't thought of that.

@skalogryz:
Thanks for the correction as you are correct. Not much use for this simple case though but, i get your point.


--- Quote ---Why to introduce a new keyword?

--- End quote ---
Well, it was the first thing that popped to mind. As minesadorada noticed correctly, i once started out with basic and some things seem to have survived my decaying brain-cells (that is, assuming i have any to begin with  ;D).

Your idea/suggestion is just as valid.


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

--- End quote ---
Yes you are correct.

As a matter of fact that is enclosed in the Pascal for construction to begin with.

The moment downto was introduced it causes ambiguity. Some basic language variants solved that by interpreting the values.

Assuming the step value is a signed integer your solution suffers from that as well.

Probably reason why something like it was not implemented/supported already ?


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

--- End quote ---
It is just syntax sugar as things can be solved using other another approach.

However, support for something like this can make the life of a programmer a bit easier (or make the programmer more lazy, depending on how you look at it).

There is a reason why OP asked his/her question. Most likely because such construct was encountered in another language before looking at Pascal.

Although i dislike most suggestions made to improve the language because it really is syntax sugar alone or an attempt to whore the language itself, it is imo a bit strange to keep having to say no to someone asking such a question. Apparently there is an expectation that such a construct is available.

Even though the enumerator solution is able to solve that partly, it isn't a basic solution that a beginner would look at (i overlooked it as well)

But, i have a feeling that this discussion was made long before it was brought up here. I simply haven't looked at any of them yet. Would be nice to have some idea on the reason(s) why it was rejected (if any).

Kays:

--- Quote from: Kays on December 15, 2016, 02:52:05 am ---[…]How do I make a for-loop iterate with a custom step width? So everything regarding iteration is in the top line.[…]
--- End quote ---
OK, for the record: It is not possible. I didn't found it, because it's not there.


--- Quote from: Handoko on December 15, 2016, 05:00:13 am ---[…]
--- 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 5 dobegin  idx := i * 2;[…]
--- End quote ---
Yeah a solution w/ one less line I haven't thought of. So I correct my lower limit: You have to spend two or three lines of code, if the loop needs a custom step width. On the other hand, (IMO) it loses some readability.


--- Quote from: molly on December 15, 2016, 07:36:24 am ---[… construct an array of indices …]
--- End quote ---

--- Quote from: BeniBela on December 15, 2016, 12:30:25 pm ---[… implement IEnumerate …]
--- End quote ---
Gee, molly and BeniBela, you both overreached the goal. No offense, but I'd be better off with the three line surrogate statement.


--- Quote from: skalogryz on December 15, 2016, 02:04:57 pm ---[…]But what algorithms require "step"? ... or is just for educational tasks?
--- End quote ---
Yeah, this question raised during education. So?


--- Quote from: molly on December 16, 2016, 04:45:02 pm ---[…]Although i dislike most suggestions made to improve the language because it really is syntax sugar alone or an attempt to whore the language itself, it is imo a bit strange to keep having to say no to someone asking such a question. Apparently there is an expectation that such a construct is available.
--- End quote ---
Exactly.


While we're at formulating Xmas wishes: I wouldn't be satisfied with a linear step width alone. E.g.:
--- Code: C  [+][-]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 ≔ 1; i < 1234; i ≔ 2 * i) { … }

molly:

--- Quote from: Kays on December 19, 2016, 12:52:33 am ---OK, for the record: It is not possible. I didn't found it, because it's not there.

--- End quote ---
That is correct.

Pascal does not have such construct as you showed in your initial post, or in your post i now reply to for that matter.


--- Quote ---Gee, molly and BeniBela, you both overreached the goal.

--- End quote ---
It is a bit over the top, yes. In the discussion the opinion was raised that it could not be done using a for loop. Two examples (how even unlikely to be practical for you as  expressed) show it is possible, just not in the way you expected  :)


--- Quote ---While we're at formulating Xmas wishes: I wouldn't be satisfied with a linear step width alone. E.g.:
--- Code: C  [+][-]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 ≔ 1; i < 1234; i ≔ 2 * i) { … }
--- End quote ---
Yes, but that is actually a while loop (or repeat until in case that has your preference).

In case of applying the while solution the lower and upper bounds (usually) also makes more sense (in comparison to Handoko's solution). Although it of course depends on the practical situation (sometimes Handoko's solution makes the code better readable/understandable).


--- Code: ---i := LowerBound
while i < Upperbound  do
begin
  i := i * 2;
end;

--- End code ---

It is far from your x-mas wish though...

edit: additional reply.

Thaddy:

--- 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";}};} ---function step(var  i:integer;const astep:integer):integer;inline;begin  Result:=i;  // use then increment step  inc(i, AStep);end; var   i:integer = -2;begin  while step(i,2) < 100 do    writeln(i);   //ori :=0;  repeat    writeln(i);  until step(i,2) = 100; // or even  repeat    writeln(step(i,2));  until i = 100;end. 

Deepaak:
The counter in for loop cannot be altered by user unlike C/C++ for loop. This discussion has no ending for something that is not designed in the compiler/language. It can be done using While/Repeat loop. So go for while/repeat loop. Don't make program unnecessary complex.

Agree with @molly

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version