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.

当某些指令需要重复多次的时候就可以使用循环语句。

循环语句的类型:

  1. Count-controlled (FOR) loops
  2. Post-condition (REPEAT) loops
  3. Pre-condition (WHILE) loops

Count-controlled (FOR) loops - FOR循环#

A set number of repetitions

已知要执行多少次循环的时候一般使用FOR循环

吃一个苹果
吃一个苹果
吃一个苹果
重复三次:
吃一个苹果
结束重复
FOR index <- 1 TO 3
OUTPUT index
NEXT index
// Outputs: 1 2 3

Post-condition (REPEAT) loops - 后置条件循环#

A repetition, where the number of repeats is not known, that is completed at least once

让循环结束的条件会在循环中出现,所以循环内的内容会至少重复一次

吃一个苹果
吃一个苹果
吃一个苹果
重复一件事情:
吃一个苹果
直到吃了三个苹果
index <- 1
REPEAT
OUTPUT index
index <- index + 1
UNTIL index > 3
// Outputs: 1 2 3

Pre-condition (WHILE) loops - 前置条件循环 WHILE循环#

A repetition, where the number of repeats is not known, that may never be completed

满足某个条件下进行循环

吃一个苹果
吃一个苹果
吃一个苹果
在没有吃到三个苹果时重复:
吃一个苹果
结束重复
index <- 1
WHILE index <= 3 DO
OUTPUT index
index <- index + 1
ENDWHILE
// Outputs: 1 2 3
Iterations 迭代
https://thyrius.top/posts/cscca/pseudo/iterations/
作者
Thyrius
发布于
2024-10-15
许可协议
CC BY-NC-SA 4.0