Basic Pascal Tutorial/Chapter 3/FOR..DO/zh CN
English · Français · Български · 中文(中国) · 日本語
3Da - FOR..DO循环 (原作者: Tao Yue, 状态: 未更改)
循环是指重复执行某动作,直至满足某些条件。
有三种类型的循环:
- 固定重复 - 重复固定的次数
- 先判断 - 测试一个布尔表达式,如果为真,则进入循环
- 后判断 - 先进入循环,判断布尔表达式为真,则继续循环
在Pascal中,已知循环次数,使用for循环:
for ?????? := ??????(???) to ??????(???) do
??????;索引必须是有序数据类型,它可以在循环体内计算,但你不能更改索引值。示例:
sum := 0;
for count := 1 to 100 do
sum := sum + count;计算机会使用较长的时间来计算总和,数学家高斯有更快的方式来计算总和。(1+100=101,2+99=101,...看出规则吗?有100个数字,所以重复50次。101*50=5050。这可不是高等数学,来源属于高斯可能是杜撰的。)
在使用for-to-do时,初始值必须小于结束值,要不然循环不会执行!
更不能在循环体内修改索引值,否则会造成编译错误。
也不能更改开始与结束值(因为,并不会按着你想的那样循环)。
如果你想递减式循环,你可以使用for-downto-do:
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.
Talk about this page
Nobody has opened a bench for this page yet. Sign in to open one.
