文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>DELPHI for 语句的循环变量的特别处理

DELPHI for 语句的循环变量的特别处理

时间:2010-09-07  来源:James-yu

今天写一段收集不同币别列表的小程序,其实就是一小段循环程序,没有想到执行结果和预期完全不一样!

下面的程序根本不能得到正确的结果,按照一般理解,for 循环执行后,iCnt应该等于iCurrCount(除非被中断),可实际上iCnt永远不会等于iCurrCount,苦思不得其解,循环执行后iCnt的值是个完全不相干的数字(如-2),彻底晕倒

procedure TForm1.Button1Click(Sender: TObject);
    type CurrType=record
        Name:string;  //货币名称
        Dsd:string;   //是否第三地
    end;
var
    CurrList:array[0..19] of CurrType;   //最多10种货币(普通,第三地)
    iCurrCount:integer;

  //   得到货币和DSD组合的列表
  //
  procedure GetCurrList(CurrName,Dsd:string);
  var iCnt:integer;
  begin
      for iCnt := 0 to iCurrCount- 1 do
      begin
          if (CurrName=CurrList[iCnt].Name) and (Dsd=CurrList[iCnt].Dsd) then  //此组合已经存在,中止查找
              Break;
      end;
      if iCnt=iCurrCount then  //如果相等,说明数组中不存在此组合,添加之
      begin
          iCurrCount:=iCurrCount+1;
          CurrList[iCurrCount-1].Name:=CurrName;
          CurrList[iCurrCount-1].Dsd:=Dsd;
      end;
  end;
begin
    iCurrCount:=0;
    GetCurrList('USD','N');
    GetCurrList('USD','Y');

    GetCurrList('EUR','Y');
    EDIT1.Text:=INTTOSTR(iCurrCount);
end;

 

iCurrCount永远为0,开始以为是DELPHI优化的BUG(关闭编译优化就有正确的结果),最后查帮助才发现:(看红字)

 

A for statement, unlike a repeat or while statement, requires you to specify explicitly the number of iterations you want the loop to go through. The syntax of a for statement is 

for counter := initialValue to finalValue do statement 

or 

for counter := initialValue downto finalValue do statement 

where

  • counter is a local variable (declared in the block containing the for statement) of ordinal type, without any qualifiers.
  • initialValue and finalValue are expressions that are assignment-compatible with counter.
  • statement is a simple or structured statement that does not change the value of counter.

The for statement assigns the value of initialValue to counter, then executes statement repeatedly, incrementing or decrementing counter after each iteration. (The for...to syntax increments counter, while the for...downto syntax decrements it.) When counter returns the same value as finalValue, statement is executed once more and the for statement terminates. In other words, statement is executed once for every value in the range from initialValue to finalValue. If initialValue is equal to finalValue, statement is executed exactly once. If initialValue is greater than finalValue in a for...to statement, or less than finalValue in a for...downto statement, then statement is never executed. After the for statement terminates (provided this was not forced by a Break or an Exit procedure), the value of counter is undefined.

 

如果循环正常中止,循环变量的值是未定义的。

 

彻底晕倒了,好像著名的<borland delphi开发人员指南>讲for循环时也没有提到这点。

 

不知道其它语言是否有这个规定?请不吝指教!       (有时想,语言有太多的特点,实在不是什么好事)

相关阅读 更多 +
排行榜 更多 +
马里奥赛车世界游戏手机版下载

马里奥赛车世界游戏手机版下载

赛车竞速 下载
无畏契约皮肤开箱器手游下载

无畏契约皮肤开箱器手游下载

休闲益智 下载
旭日之城官方正版下载

旭日之城官方正版下载

策略塔防 下载