Solved

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

Posted on 2002-04-06
4
202 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
This video discusses moving either the default database or any database to a new volume.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

707 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

12 Experts available now in Live!

Get 1:1 Help Now