只要给定的条件为真,SWIFT 4编程语言中的while循环语句就会重复执行目标语句。
SWIFT 4编程语言中while循环语法为-
while condition { statement(s) }
while循环的关键点是该循环可能永远不会运行。当测试条件并且结果为false时,将跳过循环体,并执行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
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)