国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Swift > 正文

詳解Swift語言的while循環結構

2020-03-09 17:49:32
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了Swift語言的while循環結構,包括do...while循環的用法,需要的朋友可以參考下
 

Swift 編程語言中的 while 循環語句只要給定的條件為真時,重復執行一個目標語句。

語法
Swift 編程語言的 while 循環的語法是:

復制代碼代碼如下:

while condition
{
   statement(s)
}

這里 statement(s) 可以是單個語句或語句塊。condition 可以是任何表達式。循環迭代當條件(condition)是真的。 當條件為假,則程序控制進到緊接在循環之后的行。

 

數字0,字符串“0”和“”,空列表 list(),和 undef 全是假的在布爾上下文中,除此外所有其他值都為 true。否定句一個真值 !或者 not 則返回一個特殊的假值。

流程圖

詳解Swift語言的while循環結構

while循環在這里,關鍵的一點:循環可能永遠不會運行。當在測試條件和結果是假時,循環體將跳過while循環,之后的第一個語句將被執行。

示例

復制代碼代碼如下:

import Cocoa
 
var index = 10

 

while index < 20 
{
   println( "Value of index is /(index)")
   index = index + 1
}


在這里,我們使用的是比較操作符 < 來比較 20 變量索引值。因此,盡管索引的值小于 20,while 循環繼續執行的代碼塊的下一代碼,并疊加指數的值到 20, 這里退出循環。在執行時,上面的代碼會產生以下結果:

 

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

do...while循環
不像 for 和 while 循環,在循環頂部測試循環條件,do...while 循環檢查其狀態在循環的底部。

do... while循環類似于while循環, 不同之處在于 do...while 循環保證執行至少一次。

語法
在 Swift 編程語言中的 do...while 語法如下:

復制代碼代碼如下:

do
{
   statement(s);
}while( condition );

應當指出的是,條件表達式出現在循環的底部,所以在測試條件之前循環語句執行一次。如果條件為真,控制流跳回起來繼續執行,循環語句再次執行。重復這個過程,直到給定的條件為假。

 

數字 0,字符串 “0” 和 “” ,空列表 list(),和 undef 全是假的在布爾上下文中,除此外所有其他值都為 true。否定句一個真值 !或者 not 則返回一個特殊的假值。

流程圖

詳解Swift語言的while循環結構

實例

復制代碼代碼如下:

import Cocoa
 
var index = 10

 

do{
   println( "Value of index is /(index)")
   index = index + 1
}while index < 20 


當執行上面的代碼,它產生以下結果:

 

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


注:相關教程知識閱讀請移步到swift教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 台安县| 古丈县| 葫芦岛市| 科技| 嘉祥县| 德庆县| 永胜县| 东乡县| 洛阳市| 饶河县| 滨海县| 嫩江县| 青川县| 临夏市| 株洲县| 湖州市| 绥芬河市| 恩施市| 嘉荫县| 上思县| 新乡市| 施甸县| 汝南县| 辽源市| 山东省| 双柏县| 酉阳| 宝清县| 永寿县| 金平| 沽源县| 长海县| 温泉县| 宣汉县| 邵东县| 台东市| 新密市| 察哈| 芜湖市| 旬阳县| 六盘水市|