- iOS 12 Programming for Beginners
- Craig Clayton
- 254字
- 2021-07-02 15:04:57
The for...in loop
One of the most common control statements is a for...in loop. It allows you to iterate over each element in a sequence. Let's see what a for...in loop looks like:
for <value> in <sequence> { // Code here }
We start the for...in loop with for, which is proceeded by <value>. <value> is a local constant (only the for...in loop can access it) and can be any name you like. Typically, you will want to give this value an expressive name. Next, we have in, which is followed by <sequence>. <sequence> is where we want to provide it with our sequence of numbers. Let's write the following into Playgrounds:

Notice that, in our Debug Panel, we can see all of the numbers we wanted in our range.
Let's do the same for our halfClosedRange variable by adding the following:

In our Debug Panel, we can see that we get the numbers 10 through 19. One thing to note is that these two for...in loops have different variables. In the first loop, we used the value variable, and in the second one, we used the index variable. You can make these variables whatever you choose them to be.
Also, in the two preceding examples, we used constants, but we could just use the ranges within the loop. As a next step, you need to add the following:

Now, you will see 0 to 3 print inside the Debug Panel.
What if you wanted the numbers to go in reverse order? Let's input the following for...in loop:

- SD-WAN架構(gòu)與技術(shù)(第2版)
- 企業(yè)私有云建設(shè)指南
- 面向云平臺的物聯(lián)網(wǎng)多源異構(gòu)信息融合方法
- 2018網(wǎng)信發(fā)展報告
- Getting Started with Grunt:The JavaScript Task Runner
- 城市治理一網(wǎng)統(tǒng)管
- The Kubernetes Workshop
- 2小時讀懂物聯(lián)網(wǎng)
- 網(wǎng)絡(luò)設(shè)計與應用(第2版)
- 高級網(wǎng)絡(luò)技術(shù)
- 局域網(wǎng)組成實踐
- 精通SEO:100%網(wǎng)站流量提升密碼
- 計算機通信網(wǎng)絡(luò)安全
- 一本書讀懂移動物聯(lián)網(wǎng)
- 通信系統(tǒng)實戰(zhàn)筆記:無處不在的信號處理