Recent

Author Topic: FinalValue recalculation in FOR-loop  (Read 582 times)

Vittorio

  • New Member
  • *
  • Posts: 32
FinalValue recalculation in FOR-loop
« on: December 02, 2020, 11:49:27 am »
Hi there,
Sorry for the stupid question  :)
I've got such a loop and want to recalculate finalValue = TreeView1.Items.Count-1 after each cycle.

Code: Pascal  [Select][+][-]
  1. for i := 0 to TreeView1.Items.Count-1 do
  2. begin
  3.   //TreeView1.Items.Count can be increased
  4. end;
  5.  

Is it possible?

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: FinalValue recalculation in FOR-loop
« Reply #1 on: December 02, 2020, 11:51:53 am »
Use a while or repeat loop.
The end condition in a for loop is evaluated only once: when entering the loop.
This is by design and has been so always.

Bart

Thaddy

  • Hero Member
  • *****
  • Posts: 14377
  • Sensorship about opinions does not belong here.
Re: FinalValue recalculation in FOR-loop
« Reply #2 on: December 02, 2020, 11:53:27 am »
No. a for to loop is fixed to the initial values.
You can use repeat until or while, though, if the count must change
[edit posts crossed Bart. Basically same answer]

Note actually this can be done, but it involves pointer trickery and not recommended.
E.g. bad code:
Code: Pascal  [Select][+][-]
  1. {$mode delphi}{$H+}
  2. var
  3.   i:integer;
  4.   k:integer = 10;
  5. begin
  6.   for i := 0 to 10 do
  7.   begin
  8.    writeln(k);
  9.    inc(Pinteger(@k)^);
  10.    if k >= 20 then exit;
  11.   end;
  12. end.

Never do that!!!
(but it works  ;D Partially )
« Last Edit: December 02, 2020, 12:10:08 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Vittorio

  • New Member
  • *
  • Posts: 32
Re: FinalValue recalculation in FOR-loop
« Reply #3 on: December 02, 2020, 11:56:11 am »
Thank you very much!

 

TinyPortal © 2005-2018