Sign in Join
Wiki copytemporary

Revision 1 of 2, by Kai Burghardt on 8-20-2022. The page as it is now.

Basic Pascal Tutorial/Chapter 3/FOR..DO/zh CN

3Da - FOR..DO循环 (原作者: Tao Yue, 状态: 未更改)

循环是指重复执行某动作,直至满足某些条件。

有三种类型的循环:

在Pascal中,已知循环次数,使用for循环:

pascal
for 索引 := 开始(�) to 结�(大) do
  语�;

索引必须是有序数据类型,它可以在循环体内计算,但你不能更改索引值。示例:

pascal
sum := 0;
for count := 1 to 100 do
  sum := sum + count;

计算机会使用较长的时间来计算总和,数学家高斯有更快的方式来计算总和。(1+100=101,2+99=101,...看出规则吗?有100个数字,所以重复50次。101*50=5050。这可不是高等数学,来源属于高斯可能是杜撰的。)

<b>在使用for-to-do时,初始值必须小于结束值,要不然循环不会执行!

更不能在循环体内修改索引值,否则会造成编译错误。

也不能更改开始与结束值(因为,并不会按着你想的那样循环)。</b>

如果你想递减式循环,你可以使用for-downto-do:

pascal
for 索引 := 开始(大) downto 结�(�) do
  statement;

在Pascal中,for循环自增量(步长)为1。

上一页 目录 下一页

From the Free Pascal wiki, page "Basic Pascal Tutorial/Chapter 3/FOR..DO/zh CN", as it stood on 2022-08-20 after a change by Kai Burghardt, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.