Swift 中的 while 循环函数

首页 / Swift入门教程 / Swift 中的 while 循环函数

只要给定的条件为真,SWIFT 4编程语言中的while循环语句就会重复执行目标语句。

while - 语法

SWIFT 4编程语言中while循环语法为-

无涯教程网

while condition {
   statement(s)
}

while - 流程图

While Loops

while循环的关键点是该循环可能永远不会运行。当测试条件并且结果为false时,将跳过循环体,并执行while循环后的第一个语句。

while - 示例

var index=10

while index < 20 {
   print( "Value of index is\(index)")
   index=index + 1
}

这里我们使用比较运算符<将变量index的值与20进行比较。当index的值小于20时,while循环继续执行它旁边的代码块,并且只要index的值等于20,它就会退出循环。执行时,上述代码将生成以下结果-

Value of index is 10
Value of index is 11
Value of index is 12
Value of index is 13
Value of index is 14
Value of index is 15
Value of index is 16
Value of index is 17
Value of index is 18
Value of index is 19

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

从0开始学微服务 -〔胡忠想〕

Linux性能优化实战 -〔倪朋飞〕

重学前端 -〔程劭非(winter)〕

接口测试入门课 -〔陈磊〕

跟月影学可视化 -〔月影〕

物联网开发实战 -〔郭朝斌〕

AI大模型之美 -〔徐文浩〕

结构写作力 -〔李忠秋〕

互联网人的数字化企业生存指南 -〔沈欣〕

好记忆不如烂笔头。留下您的足迹吧 :)