Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2002-04-06
4
Medium Priority
?
224 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 40 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…

824 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