Solved

VB to Delphi-how to use "step"??

Posted on 2002-04-06
4
206 Views
Last Modified: 2010-04-04
Let`s see this code:
------------
for i = 1 to 100 step 2
.....
next i
------------
but in Pascal, there isn`t the keyword "step",so how can i do ?thank you very much.
--
I come from China and i 18 years old,my english is very poor.I want to make friends with you!
E_mail:fanweixiao@sina.com
0
Comment
Question by:VitaminC
4 Comments
 
LVL 7

Expert Comment

by:Motaz
ID: 6923448
Hi VitaminC
In Object Pascal there is only two types of for loop:
for i:= 1 to 10 do
and
for i:= 10 to 1 do
so that if you want to step by 2 to 100 you can do this:
for i:= 1 to 50 do
  Writeln((i - 1) * 2 + 1);

Motaz
motaz1@yahoo.com
0
 
LVL 44

Expert Comment

by:CrazyOne
ID: 6923464
Or something like this

var
  i: Integer;
begin

  i := 1;
  repeat
    DoYourCodeHere;
    Inc(i, 2);
  until i >= 100;

end;
0
 
LVL 10

Accepted Solution

by:
Jacco earned 10 total points
ID: 6923505
Motaz:
for i:= 10 downto 1 do
You forgot downto. I forget it too sometimes. But Delphi will not do anything then :-)

VitaminC:
You could also use a while loop (the third type)

i := 1;
while i <= 100 do
begin
  DoYourStuff;
  Inc(i, 2);
end;

Regards Jacco





0
 
LVL 12

Expert Comment

by:Lee_Nover
ID: 6924233
:)
if you still want the 'for' loop you could do an unoptimized func like

for I:=0 to 100 do
  if (I mod 2) = 0 then DoYourCode;

while and repeat are preferable for this kinda thing
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

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…
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

810 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