我想问一下,delphi中的for语句的步长怎么设置哟例如
for i;=0 to 100 do
 begin     
    ...............
 end
他的步长为1,如果我想把他的步长设置为2  请问怎么办

解决方案 »

  1.   

    for i:=0 to 20 div 2 do
        showmessage(inttostr(i));
      

  2.   

    for语句的步长只能为1或-1
    如果需要步长设置为2,建议使用 repeat 或 while
      

  3.   

    for i:=0 to 20 div 2 do showmessage(inttostr(2*i));
      

  4.   

    不行?我都是这样用的,不行就用while吧。嗨!又回while。
      

  5.   

    假设有:起点=A,终点=B,步长=K>1,并且A<=B。
    那么可以:
    for I := A to B do begin
      if ((I - A) mod K) = 0 then begin
        DoSomething;
      end;
    end;
    对于K<-1并且A>=B的情况,基本原理同上。
    如果对于临界点A和B有特殊处理,只需要额外增加相应的if检查即可。
      

  6.   

    for i:=0 to 100
    begininc(i,2);
    end;
      

  7.   

    for的步长就是1(或-1)
    在循环体内使用inc是通常的做法
    就是在循环体内设置步长,这样也不麻烦呀
    如:
    i := 0;
    while i <= 100
    begin
      //do something
      inc(i, 2);
    end;其中inc(i, 2)等价于 i := i +2;
    关于inc可查阅borland的帮助文件