转至内容

Rexx 编程/如何使用 Rexx/while

取自 Wikibooks,面向开放世界的开放书籍

"while" 循环是一种不确定的循环。只要某个条件为真,它们就会重复同一部分代码。它们包含可能被重复任意次数的代码。在甚至开始运行这一代码之前,while 循环会检查条件,然后在代码完成之后再次检查条件,以查看是否应该再次运行该代码。

/* Let's start our counting at 1. */
count = 1
/* The first loop will be skipped over entirely, since the condition is false. */
do while count < 0
 say "You'll never see this!"
end
/* The following loop will count to 10 and then stop because count will be 11. */
do while count <= 10
 say count
 count = count + 1
end
/* The message about apples will only be said once, while count is still 11. */
do while count < 12
 say "An apple a day keeps the doctor away."
 count = count + 1
end

请注意,如果循环条件永远不会变为 false,while 循环将不会自行停止。它将是一个无限循环。这是一个常见的错误。

/* Just keeps on saying hello. */
do while 2 < 3
 say "Hello, world!"
end
华夏公益教科书