WHILE...WEND
- 来源: https://www.freebasic.net/wiki/wikka.php?wakka=KeyPgWhilewend
- 最后更新: 2021-09-28
用于循环的控制流语句
语法
While [condition]
[statement block]
Wend描述
While 语句将使 statement block 中后续的语句集合在表达式 condition 求值为真时反复执行。
如果在首次执行 While 语句时 condition 求值为假,则跳过 statement block,执行立即在封闭的 Wend 语句之后恢复。
如果在 statement block 内遇到 Exit While 语句,则循环终止,执行立即在封闭的 Wend 语句之后恢复。如果遇到 Continue While 语句,则跳过 statement block 的其余部分,执行在 While 语句处恢复。
与所有控制流语句一样,While 语句可以嵌套,即可以在另一个 While 语句的语句块中使用。
注意:While 关键字也用于 Do...Loop 语句中以指示比较类型。以这种方式使用时,Do 语句在功能上等同于 While 语句,因此不要混淆它们各自的封闭关键字 Loop 和 Wend。
示例
在此示例中,使用 While 循环通过反向迭代字符串来将其反转。当索引小于 0 时(0 是字符串中的第一个索引),循环停止。
start GeSHi
vb
Dim As String sentence '' 要反转的字符串
sentence = "The quick brown fox jumps over the lazy dog."
Dim As String ecnetnes
Dim As Integer index
index = Len( sentence ) - 1 '' 指向最后一个字符
While( index >= 0 ) '' 在第一个字符后停止
ecnetnes += Chr( sentence[index] ) '' 将字符追加到新字符串
index -= 1
Wend
Print "original: """ ; sentence ; """"
Print "reversed: """ ; ecnetnes ; """"
End 0end GeSHi
方言差异
- 在 -lang qb 和 -lang fblite 方言中,
While..Wend循环内声明的变量与 QB 一样具有函数级作用域。 - 在 -lang fb 和 -lang deprecated 方言中,
While..Wend块内声明的变量仅在块内可见,无法在块外访问。要访问此块外定义为全局的重复符号,请添加一个或最好两个点作为前缀:.SomeSymbol或最好..SomeSymbol(或仅在 With..End With 块内使用..SomeSymbol)。
与 QB 的区别
- 无
参见
ExitContinueDo...Loop
返回 目录