我們?cè)谇懊鎸W(xué)習(xí)了循環(huán),它是用
for i in 1... n {
}
的形式實(shí)現(xiàn)的涛贯。你已經(jīng)用過它很多次了,它能幫你解決很多次數(shù)要重復(fù)做的事情蔚出,功能很強(qiáng)大弟翘。
現(xiàn)在,我們想讓循環(huán)幫我們解決一些不限制次數(shù)的事情骄酗,我們要怎么辦呢稀余?如果角色每次要收集的寶石數(shù)據(jù)都有變化,就不能再用for循環(huán)解決了趋翻,那樣子睛琳,可能會(huì)多做,也可能會(huì)少做踏烙。
這時(shí)間师骗,就要學(xué)習(xí)一種新的循環(huán)方式了,它是用條件來判斷的讨惩,判斷條件對(duì)你并不陌生辟癌,循環(huán)對(duì)你也不陌生,但能判斷條件的循環(huán)你會(huì)是第一次見到荐捻,請(qǐng)看下面:
while ?條件{
? ? ? ?要執(zhí)行的命令
}
這里的條件愿待,跟之前練習(xí)過的條件是一樣的。同時(shí)也支持運(yùn)算符的方法靴患。
while isBlocked {
? ? ? ? turnleft()
}
while !isBlocked{
? ? ? ? moveForward()
}
這一樣子一比較仍侥,你是不是立刻就明白了。
那如果條件不成立會(huì)怎么樣呢鸳君?
沒錯(cuò)农渊,條件不成立的時(shí)候,這個(gè)循環(huán)就會(huì)退出或颊。不再執(zhí)行砸紊。
這樣了,就比for循環(huán)少了次數(shù)的限制了囱挑。
這關(guān)很容易醉顽,完整代碼如下:
while ?isOnClosedSwitch && !isBlocked ?{
? ? ? toggleSwitch()
? ? ?moveForward()
}
傳送門:
MacOS Playgrounds學(xué)習(xí) 學(xué)習(xí)編程一 前記--http://www.reibang.com/p/a0de277374a4
MacOS Playgrounds 學(xué)習(xí)編程一 第一關(guān) 發(fā)出命令--http://www.reibang.com/p/3c85397f1eae
MacOS Playgrounds 學(xué)習(xí)編程一 第二關(guān) 添加新命令--http://www.reibang.com/p/4efa5c18dc50
MacOS Playgrounds 學(xué)習(xí)編程一 第三關(guān) 切換開關(guān)--http://www.reibang.com/p/00556d6430bf
MacOS Playgrounds 學(xué)習(xí)編程一 第四關(guān) 傳送門練習(xí)--http://www.reibang.com/p/47bd57d0193f
MacOS Playgrounds 學(xué)習(xí)編程一 第五關(guān) 發(fā)現(xiàn)并修復(fù)程序錯(cuò)誤--http://www.reibang.com/p/5bc9200edbbe
MacOS Playgrounds 學(xué)習(xí)編程一 第六關(guān) 消滅程序錯(cuò)誤練習(xí)--http://www.reibang.com/p/f19bd2955ed8
MacOS Playgrounds 學(xué)習(xí)編程一 第七關(guān) 最短路線--http://www.reibang.com/p/1ef210253be5
MacOS Playgrounds 學(xué)習(xí)編程一 第八關(guān) 組合新行為--http://www.reibang.com/p/f548b1fd3134
MacOS Playgrounds 學(xué)習(xí)編程一 第九關(guān) 創(chuàng)建新函數(shù)--http://www.reibang.com/p/22ab39aaea97
MacOS Playgrounds 學(xué)習(xí)編程一 第十關(guān) 收集、切換平挑、重復(fù)--http://www.reibang.com/p/e43d60f98bc7
MacOS Playgrounds 學(xué)習(xí)編程一 第十二關(guān) 嵌套模式--http://www.reibang.com/p/645f6716217c
MacOS Playgrounds 學(xué)習(xí)編程一 第十三關(guān) 嵌入式階梯--http://www.reibang.com/p/d87f9662601d
MacOS Playgrounds 學(xué)習(xí)編程一 第十四關(guān) 尋寶--http://www.reibang.com/p/18c73154e46b
MacOS Playgrounds 學(xué)習(xí)編程一 第十五關(guān) 使用循環(huán)--http://www.reibang.com/p/f7c0866930ec
MacOS Playgrounds 學(xué)習(xí)編程一 第十六關(guān) 循環(huán)每一側(cè)--http://www.reibang.com/p/94d75e9d5eab
MacOS Playgrounds 學(xué)習(xí)編程一 第十七關(guān) 行至邊緣再返回--http://www.reibang.com/p/cc7e8095bb9a
MacOS Playgrounds 學(xué)習(xí)編程一 第十八關(guān) 循環(huán)跳躍者--http://www.reibang.com/p/99618bef896e
MacOS Playgrounds 學(xué)習(xí)編程一 第十九關(guān) 拓展技能--http://www.reibang.com/p/a20810b11d6d
MacOS Playgrounds 學(xué)習(xí)編程一 第二十關(guān) 寶石農(nóng)場--http://www.reibang.com/p/8c3808d1a646
MacOS Playgrounds 學(xué)習(xí)編程一 第二十一關(guān) 席卷四處--http://www.reibang.com/p/cc7f6a1c34e7
MacOS Playgrounds 學(xué)習(xí)編程一 第二十二關(guān) 檢查開關(guān)--http://www.reibang.com/p/e34fb0431b6e
MacOS Playgrounds 學(xué)習(xí)編程一 第二十三關(guān) 使用 else if--http://www.reibang.com/p/6414ef7692e2
MacOS Playgrounds 學(xué)習(xí)編程一 第二十四關(guān) 循環(huán)條件代碼--http://www.reibang.com/p/a657e47b851d
MacOS Playgrounds 學(xué)習(xí)編程一 第二十五關(guān) 滿足條件時(shí)向上爬--http://www.reibang.com/p/4a69a1c3435f
MacOS Playgrounds 學(xué)習(xí)編程一 第二十六關(guān) 定義更巧妙的函數(shù)--http://www.reibang.com/p/d61e03e095ae
MacOS Playgrounds 學(xué)習(xí)編程一 第二十七關(guān) 圍困其中--http://www.reibang.com/p/88442c7ce61b
MacOS Playgrounds 學(xué)習(xí)編程一 第二十八關(guān) 決策樹--http://www.reibang.com/p/0843bd9ae286
MacOS Playgrounds 學(xué)習(xí)編程一 第二十九關(guān) 使用“非”運(yùn)算符--http://www.reibang.com/p/be8fa74b6c46
MacOS Playgrounds 學(xué)習(xí)編程一 第三十關(guān) 非之螺旋--http://www.reibang.com/p/f42905cbaed1
MacOS Playgrounds 學(xué)習(xí)編程一 第三十一關(guān) 檢查這個(gè)與那個(gè)--http://www.reibang.com/p/67b1c6e16b6a
MacOS Playgrounds 學(xué)習(xí)編程一 第三十二關(guān) 檢查這個(gè)或那個(gè)--http://www.reibang.com/p/d94a67728a93
MacOS Playgrounds 學(xué)習(xí)編程一 第三十三關(guān) 邏輯迷宮--http://www.reibang.com/p/62bbc0ce2490
MacOS Playgrounds 學(xué)習(xí)編程一 第三十四關(guān) 當(dāng)……時(shí)運(yùn)行代碼--http://www.reibang.com/p/9aa41733c6d0
MacOS Playgrounds 學(xué)習(xí)編程一 第三十五關(guān) 創(chuàng)建更巧妙的while循環(huán)--http://www.reibang.com/p/3fdfb33a443f
MacOS Playgrounds 學(xué)習(xí)編程一 第三十六關(guān) 正確選取工具--http://www.reibang.com/p/e5ed25be735e
MacOS Playgrounds 學(xué)習(xí)編程一 第三十七關(guān) 四乘以四--http://www.reibang.com/p/81ed2e6009d4
MacOS Playgrounds 學(xué)習(xí)編程一 第三十八關(guān) 轉(zhuǎn)身--http://www.reibang.com/p/a3c9bae7d6a2
MacOS Playgrounds 學(xué)習(xí)編程一 第三十九關(guān) 富饒之地--http://www.reibang.com/p/077743cdfcdd
MacOS Playgrounds 學(xué)習(xí)編程一 第四十關(guān) 套嵌循環(huán)--http://www.reibang.com/p/d8670c9e7962
MacOS Playgrounds 學(xué)習(xí)編程一 第四十一關(guān) 隨機(jī)矩形--http://www.reibang.com/p/61b916f61e37
MacOS Playgrounds 學(xué)習(xí)編程一 第四十二關(guān) 始終右轉(zhuǎn)--http://www.reibang.com/p/b7cf92fbfcaa
MacOS Playgrounds 學(xué)習(xí)編程一 第四十三關(guān) 右手定則--http://www.reibang.com/p/9ccd20151c96
MacOS Playgrounds 學(xué)習(xí)編程一 第四十四關(guān) 調(diào)整算法--http://www.reibang.com/p/10c2a0de990d
MacOS Playgrounds 學(xué)習(xí)編程一 第四十五關(guān) 征服迷宮--http://www.reibang.com/p/b4d731fe3639
MacOS Playgrounds 學(xué)習(xí)編程一 第四十六關(guān) 左轉(zhuǎn)還是右轉(zhuǎn)--http://www.reibang.com/p/cd7fa32316e5
MacOS Playgrounds 學(xué)習(xí)編程一 第四十七關(guān) 向左走游添,向右走--http://www.reibang.com/p/b9b1dc9c8f2e
MacOS Playgrounds學(xué)習(xí) 學(xué)習(xí)編程一 小結(jié)后記--http://www.reibang.com/p/8ad6485e447a