Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

For loop descending instead of ascending

Posted on 2003-10-27
2
Medium Priority
?
1,221 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 400 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 400 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

618 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