Solved

For loop descending instead of ascending

Posted on 2003-10-27
2
1,199 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now