Recent

Author Topic: [Solved] Usage of for-loop var after the loop  (Read 1830 times)

AlexTP

  • Hero Member
  • *****
  • Posts: 2519
    • UVviewsoft
[Solved] Usage of for-loop var after the loop
« on: February 19, 2023, 09:31:24 pm »
Snippet

Code: Pascal  [Select][+][-]
  1. for i:= 1 to 100 do
  2. begin
  3.   if SMTH1 then break;
  4.   SMTH2;
  5. end;
  6.  
  7. if i<100 then // ?
  8.   SMTH3;
  9.  
Can I read 'i' after the for-loop?
« Last Edit: February 19, 2023, 09:47:37 pm by AlexTP »

Jonas Maebe

  • Hero Member
  • *****
  • Posts: 1067
Re: Usage of for-loop var after the loop
« Reply #1 on: February 19, 2023, 09:37:43 pm »
Can I read 'i' after the for-loop?
It's only guaranteed to be defined if "break" triggered. So if it didn't trigger, then "i" could have any value, including 0 - 99.

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Usage of for-loop var after the loop
« Reply #2 on: February 19, 2023, 09:39:28 pm »
Can I read 'i' after the for-loop?
It's only guaranteed to be defined if "break" triggered. So if it didn't trigger, then "i" could have any value, including 0 - 99.
When it was not triggered it is 100.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

TRon

  • Hero Member
  • *****
  • Posts: 3778
Re: Usage of for-loop var after the loop
« Reply #3 on: February 19, 2023, 09:39:39 pm »
Can I read 'i' after the for-loop?
Ofc you can  :)

The question is does it make sense if you do. So let's try the documentation to see if that answers any questions: https://www.freepascal.org/docs-html/ref/refsu58.html
Quote
The value of the loop variable is undefined after a loop has completed or if a loop is not executed at all. However, if the loop was terminated prematurely with an exception or a break or goto statement, the loop variable retains the value it had when the loop was exited.
I do not have to remember anything anymore thanks to total-recall.

jamie

  • Hero Member
  • *****
  • Posts: 6787
Re: Usage of for-loop var after the loop
« Reply #4 on: February 19, 2023, 11:08:40 pm »
Can I read 'i' after the for-loop?
It's only guaranteed to be defined if "break" triggered. So if it didn't trigger, then "i" could have any value, including 0 - 99.

I don't think that is a good idea?

 You really don't know how it exited the loop ? Lets say I called BREAK on 100 of the loop!

 And basically, the loop in most cases would end reporting at 100 from outside when completed, but the rules state not to rely on that. it's what the compiler wants to do with it from revision to code complexity.

 I think it would be a better idea to simply not rely on it, personally.

 Use a WHILE....

The only true wisdom is knowing you know nothing

440bx

  • Hero Member
  • *****
  • Posts: 4889
Re: Usage of for-loop var after the loop
« Reply #5 on: February 20, 2023, 12:58:28 am »
Use a WHILE....
That's the best advice.

The Pascal "for" loop is defined in such a way as to allow the compiler to easily optimize the loop (by putting the counter in a CPU register.)  If a programmer wants/needs to depend on the value of the counter, a Pascal "for" loop is very far from ideal.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

dseligo

  • Hero Member
  • *****
  • Posts: 1443
Re: [Solved] Usage of for-loop var after the loop
« Reply #6 on: February 20, 2023, 12:06:00 pm »
Snippet

Code: Pascal  [Select][+][-]
  1. for i:= 1 to 100 do
  2. begin
  3.   if SMTH1 then break;
  4.   SMTH2;
  5. end;
  6.  
  7. if i<100 then // ?
  8.   SMTH3;
  9.  
Can I read 'i' after the for-loop?

If you want to use for loop and be sure that 'i' will be correct after the loop, you could do this:

Code: Pascal  [Select][+][-]
  1. for i:= 1 to 100 do
  2. begin
  3.   if SMTH1 then break;
  4.   SMTH2;
  5.  
  6.   // add above final loop 'end':
  7.   If i = 100 then break;
  8. end;
  9.  
  10. if i<100 then // ?
  11.   SMTH3;
  12.  

Thaddy

  • Hero Member
  • *****
  • Posts: 16348
  • Censorship about opinions does not belong here.
Re: [Solved] Usage of for-loop var after the loop
« Reply #7 on: February 20, 2023, 01:21:23 pm »
If you want to use for loop and be sure that 'i' will be correct after the loop, you could do this:
That is simply an assumption about internals. It has been answered many times on this forum by e.g. Marcov and PascalDragon or Jonas' answer above.
The true answer is that the value of the loop variable is undefined after the loop has finished and you can not rely on its value., that is unless the loop is aborted by exit() or Break, then the value is defined.

Side note: Delphi and FreePascal will give different results, but BOTH are undefined.

You can ping PascalDragon, but you will get the same answer..
« Last Edit: February 20, 2023, 01:29:56 pm by Thaddy »
There is nothing wrong with being blunt. At a minimum it is also honest.

Kays

  • Hero Member
  • *****
  • Posts: 614
  • Whasup!?
    • KaiBurghardt.de
Re: [Solved] Usage of for-loop var after the loop
« Reply #8 on: February 20, 2023, 01:30:00 pm »
[…] you could do this:
Code: Pascal  [Select][+][-]
  1. for i:= 1 to 100 do
  2. begin
  3.   if SMTH1 then break;
  4.   SMTH2;
  5.  
  6.   // add above final loop 'end':
  7.   If i = 100 then break;
  8. end;
Checking 100 times for equality for some predictable condition? That’s an anti-pattern. You simply need to assign the final value outside/before the for-loop:
Code: Pascal  [Select][+][-]
  1. i := 100;
  2. for i := 1 to i do
  3. begin
  4.         { … }
  5. end;
Yours Sincerely
Kai Burghardt

dseligo

  • Hero Member
  • *****
  • Posts: 1443
Re: [Solved] Usage of for-loop var after the loop
« Reply #9 on: February 20, 2023, 04:38:05 pm »
If you want to use for loop and be sure that 'i' will be correct after the loop, you could do this:
That is simply an assumption about internals.

There is no assumption. It isn't efficient and that's why I wrote 'If you want to use for loop'.

dseligo

  • Hero Member
  • *****
  • Posts: 1443
Re: [Solved] Usage of for-loop var after the loop
« Reply #10 on: February 20, 2023, 04:39:50 pm »
Checking 100 times for equality for some predictable condition? That’s an anti-pattern. You simply need to assign the final value outside/before the for-loop:
Code: Pascal  [Select][+][-]
  1. i := 100;
  2. for i := 1 to i do
  3. begin
  4.         { … }
  5. end;

I didn''t say it is a good solution.
And I think that your solution is not guaranteed to be working.

Zvoni

  • Hero Member
  • *****
  • Posts: 2793
Re: [Solved] Usage of for-loop var after the loop
« Reply #11 on: February 20, 2023, 05:06:32 pm »
So, bottom line (to get this thread done):
if you want to check the value of the iteration-variable of a For-loop if the loop exits prematurely, use a separate "counting"-variable, and don't rely on the iteration-variable itself.

Or use a Do/While - Repeat/Until
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Kays

  • Hero Member
  • *****
  • Posts: 614
  • Whasup!?
    • KaiBurghardt.de
Re: Usage of for-loop var after the loop
« Reply #12 on: February 20, 2023, 05:30:02 pm »
[…] And I think that your solution is not guaranteed to be working.
Well, ISO standard 7185 “Standard Pascal” says
Quote
[…] After a for‑statement is executed, other than being left by a goto‑statement, the control-variable shall be undefined. […]
I don’t think this means “undefine (verb) counter variable”, but the specific value is not defined by the for-statement. If you define (assign) a value outside the for-loop, it certainly remains defined. The for-loop does not invalidate any previous value the counter-variable held.

However, as 440bx already remarked, the counter variable may be stored as a processor register, say ecx. Only a goto will ensure this volatile value will be written back into the respective variable’s memory location.

Meanwhile, it’s FPC we’re talking about. The documentation says it pretty clearly:
Quote
  • The value of the loop variable is undefined after a loop has completed or if a loop is not executed at all. However, if the loop was terminated prematurely with an exception or a break or goto statement, the loop variable retains the value it had when the loop was exited.
Yours Sincerely
Kai Burghardt

PascalDragon

  • Hero Member
  • *****
  • Posts: 5802
  • Compiler Developer
Re: Usage of for-loop var after the loop
« Reply #13 on: February 20, 2023, 11:33:37 pm »
Can I read 'i' after the for-loop?
It's only guaranteed to be defined if "break" triggered. So if it didn't trigger, then "i" could have any value, including 0 - 99.
When it was not triggered it is 100.
This is not guaranteed. An optimization might change that (and the compiler is in fact allowed to do that!). Only a Break, GoTo or exception ensures that the counter retains its value after the loop.


 

TinyPortal © 2005-2018