Solved

For loop descending instead of ascending

Posted on 2003-10-27
2
1,204 Views
Last Modified: 2008-02-07
hi,

My problem is with a standard for loop, tried on different pcs, but it is counting down from 100 to 1 and not from 1 to 100.

What is the problem???

x : integer;

for x := 1 to 100 do
begin
   XXXXXXX
end


Thanks
0
Comment
Question by:stuart313
2 Comments
 
LVL 27

Accepted Solution

by:
kretzschmar earned 100 total points
ID: 9626617
thats just because the optimize-flag is set to on in the compileroptions and a descanding count is faster than an ascanding count

-> workarounds
-disable optimeizerflag
or
use your loopvar within the loop

meikl ;-)
0
 
LVL 17

Assisted Solution

by:geobul
geobul earned 100 total points
ID: 9626642
Hi,

You do not use 'x' variable inside the loop and the compiler optimizes your code for speed, i.e. 100 downto 1 is faster than 1 to 100. Example:

var x,y: integer;
begin
  y := 10;
  for x := 1 to 5 do begin
    ShowMessage(IntToStr(y));
  end;
end;

the loop counts from 5 downto 1. But:

var x: integer;
begin
  for x := 1 to 5 do begin
    ShowMessage(IntToStr(x));
  end;
end;

counts from 1 to 5. 'x' is used inside the loop.

Regards, Geo
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question