Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the pascal for loop work?

Tags:

pascal

I've got a question related to the way the for loop works in Pascal:

Program example;
var i:integer;
Begin
     i:=7;
     for i:=1 to i do write(i);
End.

This piece of code outputs: 1234567. I think that the compiler makes a secondary copy of the variable i, and then uses that one in the for loop.

like image 435
LeDev Avatar asked Oct 14 '25 09:10

LeDev


1 Answers

Note that this is REQUIRED to work in Pascal according to the ISO7185 standard. The "end value" should be copied before the loop counter is modified.

https://github.com/Leporacanthicus/lacsap/blob/master/test/Basic/iso7185pat.pas#L761

like image 119
Mats Petersson Avatar answered Oct 17 '25 23:10

Mats Petersson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!