Solved

For loop descending instead of ascending

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

696 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