340 字
2 分钟
Iterations 迭代
When some actions performed as part of an algorithm need repeating this is called iteration. Loop structures are used to perform the iteration.
当某些指令需要重复多次的时候就可以使用循环语句。
循环语句的类型:
- Count-controlled (FOR) loops
- Post-condition (REPEAT) loops
- Pre-condition (WHILE) loops
Count-controlled (FOR) loops - FOR循环
A set number of repetitions
当已知要执行多少次循环的时候一般使用FOR循环
吃一个苹果吃一个苹果吃一个苹果重复三次: 吃一个苹果结束重复FOR index <- 1 TO 3 OUTPUT indexNEXT index// Outputs: 1 2 3Post-condition (REPEAT) loops - 后置条件循环
A repetition, where the number of repeats is not known, that is completed at least once
让循环结束的条件会在循环中出现,所以循环内的内容会至少重复一次
吃一个苹果吃一个苹果吃一个苹果重复一件事情: 吃一个苹果直到吃了三个苹果index <- 1REPEAT OUTPUT index index <- index + 1UNTIL index > 3// Outputs: 1 2 3Pre-condition (WHILE) loops - 前置条件循环 WHILE循环
A repetition, where the number of repeats is not known, that may never be completed
在满足某个条件下进行循环
吃一个苹果吃一个苹果吃一个苹果在没有吃到三个苹果时重复: 吃一个苹果结束重复index <- 1WHILE index <= 3 DO OUTPUT index index <- index + 1ENDWHILE// Outputs: 1 2 3 Iterations 迭代
https://thyrius.top/posts/cscca/pseudo/iterations/