在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
今天写一段收集不同币别列表的小程序,其实就是一小段循环程序,没有想到执行结果和预期完全不一样! 下面的程序根本不能得到正确的结果,按照一般理解,for 循环执行后,iCnt应该等于iCurrCount(除非被中断),可实际上iCnt永远不会等于iCurrCount,苦思不得其解,循环执行后iCnt的值是个完全不相干的数字(如-2),彻底晕倒 procedure TForm1.Button1Click(Sender: TObject); // 得到货币和DSD组合的列表 GetCurrList('EUR','Y');
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
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循环时也没有提到这点。
for 循环标准的执行流程
不知道其它语言是否有这个规定?请不吝指教! (有时想,语言有太多的特点,实在不是什么好事) |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论