Sign in Join
Wiki copytemporary

Basic Pascal Tutorial/Chapter 3/FOR..DO/zh CN: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
113Da - FOR..DO循环 (原作者: Tao Yue, 状态: 未更改)
22
33循环是指重复执行某动作,直至满足某些条件。
44
55
66有三种类型的循环:
77
88 * 固定重复 - 重复固定的次数
99 * 先判断 - 测试一个布尔表达式,如果为真,则进入循环
1010 * 后判断 - 先进入循环,判断布尔表达式为真,则继续循环
1111
1212
1313在Pascal中,已知循环次数,使用for循环:
1414```
1515for 索引 := 开始(小) to 结束(大) do
1616 语句;
1717```
1818
1919索引必须是有序数据类型,它可以在循环体内计算,但你不能更改索引值。示例:
2020```
2121sum := 0;
2222 for count := 1 to 100 do
2323 sum := sum + count;
2424```
2525
2626计算机会使用较长的时间来计算总和,[数学家高斯](<http://zh.wikipedia.org/wiki/%E5%8D%A1%E7%88%BE%C2%B7%E5%BC%97%E9%87%8C%E5%BE%B7%E9%87%8C%E5%B8%8C%C2%B7%E9%AB%98%E6%96%AF>)有更快的方式来计算总和。(`1+100=101,2+99=101`,...看出规则吗?有100个数字,所以重复50次。`101*50=5050`。这可不是高等数学,来源属于高斯可能是杜撰的。)
2727
2828
2929**在使用` for-to-do`时,初始值必须小于结束值,要不然循环不会执行! **
3030
3131**更不能在循环体内修改索引值,否则会造成编译错误。**
3232
3333**也不能更改开始与结束值(因为,并不会按着你想的那样循环)。**
3434
3535
3636如果你想递减式循环,你可以使用`for-downto-do`:
3737```
3838for 索引 := 开始(大) downto 结束(小) do
3939 statement;
4040```
4141
4242在Pascal中,for循环自增量(步长)为1。
4343
4444[[Basic Pascal Tutorial/Chapter 3/CASE/zh CN|上一页]] | [[Basic Pascal Tutorial/Contents/zh CN|目录]] | [[Basic Pascal Tutorial/Chapter 3/WHILE..DO/zh CN|下一页]]
4545---|---|---
4646
4747---
48-*From the Free Pascal wiki, page "FOR..DO/zh CN", as the Internet Archive kept it on 2023-02-02. 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](/wiki-contact).*
48+*From the Free Pascal wiki, page "FOR..DO/zh CN", as the Internet Archive kept it on 2023-02-02, last changed on 2022-08-20. 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](/wiki-contact).*

Back to history