Solved

For loop descending instead of ascending

Posted on 2003-10-27
2
1,212 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

726 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