swift 循环

In this tutorial, we’ll be looking into the wide variety of statements that Swift has to offer. We’ll be largely covering swift for loop, swift while, repeat-while and switch statements. Open up the playground and let’s dive right in it. Earlier we looked into Swift array. Furthermore we’ll be looking at the newly introduced One Sided Ranges with Swift 4.

在本教程中,我们将研究Swift必须提供的各种语句。 我们将主要介绍swift for loopswift whilerepeat-whileswitch语句。 打开操场,让我们在其中潜水。 之前,我们研究了Swift数组 。 此外,我们将研究带有Swift 4的新推出的One Sided Ranges。

If you’d like to use an online compiler for Swift, go for https://iswift.org/playground.

如果您想为Swift使用在线编译器,请访问https://iswift.org/playground 。

Swift for循环 (Swift for loop)

To iterate over a sequence without using subscripts (indexes) we use for loop as shown below.

要遍历序列而不使用下标(索引),我们使用for loop ,如下所示。

var numberArray = [2,4,6,8,10]
for number in numberArray {print(number)
}

for-in loops using subscripts with lower and upper bounds

使用带有下限和上限的下标的for-in循环

var numberArray = [2,4,6,8,10]
for i in lowerbound...upperbound
{//do something
}
//example : 1
var numberArray = [2,4,6,8,10]
for i in 0...4
{print(numberArray[i])
}

The array gets iterated from the lowerbound to the upperbound (both inclusive) using closed range operator (...). To iterate with the upper bound not included, we use the half-range operator (..<). An example is given below:

使用封闭范围运算符(...)将数组从下限迭代到上限(包括两端(...) 。 为了不包含上限而进行迭代,我们使用半角运算符(..<) 。 下面是一个示例:

for i in 0..<4
{print(numberArray[i]) //doesn't print 10
}

Note: If lowerbound > upperbound there’ll be a crash.
To print the array in reverse order we can use the reversed() function as shown below.

注意 :如果lowerbound > upperbound将会崩溃。
要以相反的顺序打印数组,我们可以使用reversed()函数,如下所示。

for i in (0..<4).reversed()
{print(numberArray[i])
}
// the below code will crash
for i in 4..<0
{print(numberArray[i])
}

stride is a function from the Swift library that allows us to enter the start value, end value and the offset value to increment by as shown below:

stride是Swift库中的一个函数,可让我们输入起始值,结束值和偏移值以增加如下所示:

//count from 1 to 10 by 1
for i in stride(from: 1, to: 10, by: 1) { print(i)
} //count from 1 to 10 by 2
for i in stride(from: 1, to: 10, by: 2) { print(i)
}
prints:
1
3
5
7
9

Ignoring value from each sequence

忽略每个序列的值

for _ in 1...5 {print("Hello World")
}

Underscore effectively gets rid of the value from each sequence. This usage is similar to while loop and can be used for calculating the power of a number as shown below:

下划线有效地消除了每个序列的值。 此用法类似于while循环,可用于计算数字的幂,如下所示:

let base = 3
let power = 10
var answer = 1
for _ in 1...power {answer *= base
}
print("\(base) to the power of \(power) is \(answer)")

While循环 (While loop)

while loops through it’s body of statements until the condition becomes false.

while循环遍历语句的主体,直到条件变为假。

var i = 0
while i <= 5 {print(i)i = i + 1
}

Note: conditions present in all the control flow statements such as while and for-in loops, if else in Swift, unlike other languages aren’t enclosed in parentheses ().

注意 :所有控制流语句中的条件(例如whilefor-in循环)( if else是Swift的if else存在,与其他语言不同的是,括号()中没有包含这些条件。

repeat-while loops while a condition is met. The difference between a while and a repeat-while loop is that the repeat loop executes the statements present in the body before checking the condition.

满足条件时重复循环。 whilerepeat-while循环之间的区别在于,repeat循环在检查条件之前执行主体中存在的语句。

var i = 5
repeat {print(i)i = i + 1
} while i < 5

Note: repeat-while loop is similar to the do-while loop in C, JAVA.

注意repeat-while循环类似于C,JAVA中的do-while循环。

切换语句 (Switch statements)

A switch statement considers a value and compares it against several possible matching patterns. An example is given below:

switch语句考虑一个值,并将其与几种可能的匹配模式进行比较。 下面是一个示例:

let character: Character = "a"
switch character {
case "a":print("The first letter of the alphabet") //this gets printed.
case "z":print("The last letter of the alphabet")
default:print("Some other character")
}

Unlike other languages switch statements in swift, finish as soon as the first case is matched. They don’t fallthrough other cases or require an explicit break statement.
To explicitly fallthrough the cases the fallthrough keyword is required as shown under:

与其他语言Swift切换语句不同,第一种情况匹配后立即完成。 它们不会遇到其他情况,也不需要明确的break语句。
要明确发现情况,需要fallthrough关键字,如下所示:

let character: Character = "a"
switch character {
case "a":print("The first letter of the alphabet") //this gets printed.fallthrough
case "z":print("The last letter of the alphabet")//this gets printed toofallthrough
default:print("Some other character") //this gets printed too
}

Combining multiple cases in switch
Swift allows multiple cases to be appended in switch (separated by commas) as shown below.

组合多个案例
Swift允许将多个案例添加到switch中(用逗号分隔),如下所示。

var myInt = 0switch myInt
{
case 0, 1, 2:print("zero, one or two") //this gets printed.case 3,5,7:print("three, five or seven")case 4:print("four")default:print("Integer out of range")
}

Interval matching in Switch
Values in switch cases can be checked for their inclusion in a certain range as shown below:

交换机中的间隔匹配
可以检查切换案例中的值是否包含在特定范围内,如下所示:

var myInt = 5switch (myInt)
{
case 0...5:print("First half") //this gets printedcase 5..<10:print("Second half")default:print("Out of range")
}

In the above code “First Half” is printed. Though the number 5 exists in both the cases, the first is printed since its met first.
The second case ranges from 5 to 10 where 10 is not inclusive.

在上面的代码中,“上半部分”被打印出来。 尽管在这两种情况下都存在数字5,但是从第一个遇见以来就打印第一个。
第二种情况是5到10,其中不包括10。

Using where statement

使用where语句

var myInt = 5switch (myInt)
{
case 0...5 where myInt%2==0:print("First half only even accepted")case 5..<10:print("Second half only odd accepted")default:print("Out of range")
}

The where keyword is used to add an additional criteria inside the case.

where关键字用于在案例中添加其他条件。

单面射程 (One-Sided Ranges)

Swift 4 has introduced one-sided ranges wherein the missing range is automatically inferred.
Following example demonstrate the same.

斯威夫特4(Swift 4)引入了一个单边范围,可自动推断出缺失范围。
下面的示例演示相同。

let stringArray = ["How", "you", "doing", "today", "??"]let lowerHalf = stringArray[..<2] //["How", "you"]
let upperHalf = stringArray[2...] //["doing", "today", "??]

This brings an end to this tutorial. We’ve discussed and implemented all the major control statements.

本教程到此结束。 我们已经讨论并实现了所有主要的控制声明。

翻译自: https://www.journaldev.com/15293/swift-for-loop-switch-while

swift 循环

swift 循环_Swift进行循环,切换,同时相关推荐

  1. 大漠多账号循环登录任务自动切换模板大漠绑定后台绑定游戏

    采用了一键后台绑定游戏窗口,包含了游戏窗口句柄的获取,采用EnumWindowByProcess枚举窗口句柄,采用SetWindowState 激活窗口,绑定可以根据自己的游戏,这里采用BindWin ...

  2. 大漠多账号循环登录任务自动切换模板免注册调用大漠

    免注册调用大漠类模块,采用了大漠VIP类模块生成工具,快速导入易语言,生成大漠的类模块,免注册的好处,就是不用注册到系统,减少杀软的拦截. 免注册调用大漠源码: .版本 2 .支持库 iext.局部变 ...

  3. [UE]初学入门1——灯光切换、烟雾显示切换、循环打印、For循环、While循环、为Array数组添加变量、宏、接触物体产生爆炸、

    [UE4]初学入门--Event类型节点 软件 UE安装 UE官网下载链接: UE安装教程: UE新建项目 EventBeginPlay节点 游戏运行时灯光关闭 OnActorBeginOverlap ...

  4. 【小河今学 | JavaScript + JQuery】音乐播放器4-音量调整、单曲循环、全部循环

    08.18今天来完善音乐播放器的音量调整.单曲循环和全部循环功能. 本篇基于上一篇[小河今学 | JavaScript + JQuery]音乐播放器3-歌词轮播和进度条跳转功能实现 一.音量调整 首先 ...

  5. c语言外循环和内循环区别是什么意思,内循环和外循环的区别 内循环和外循环的正确使用方法...

    过完五一,天气要慢慢热起来了,汽车空调使用的季节又要来了,但是很多车主对汽车空调的内外循环使用还是有点傻傻分不清,今天汽车维修网小编就和大家简单的说一下内循环和外循环的 内循环和外循环标志 内循环和外 ...

  6. Linux高级专题详解--shell编程大全(shell变量,if语句,case语句,for循环,while循环,函数调用,数组,正则表达式,shell脚本三剑客--grep,sed,awk家族)

    shell编程 初始shell 程序 语言 编程 ---------------------------------- 语言 自然语言:汉语.英语 计算机语言:c语言.c++.(java php py ...

  7. python哨兵循环_Python通用循环的构造方法实例分析

    本文实例讲述了python通用循环的构造方法.分享给大家供大家参考,具体如下: 1.交互循环 是无限循环的一种,允许用户通过交互的方式程序的特定部分: def main(): sum =0.0 cou ...

  8. java循环使用范围_Java循环流程控制语句

    7 循环流程控制语句 7.1 for循环的格式及基本使用 7.1.1 for循环语句格式: for(初始化语句;判断条件语句;控制条件语句){ 循环体语句; } 7.1.2 循环的执行流程图: 案例: ...

  9. python当型循环_对python while循环和双重循环的实例详解

    废话不多说,直接上代码吧! #python中,while语句用于循环执行程序,即在某个条件下,循环执行某段程序,以处理需要重复处理的相同任务. #while是"当型"循环结构. i ...

最新文章

  1. @字王2012·纵变体系列,共12款
  2. UVA11100旅行(大包装小包,问最少多少个包)
  3. java ssh过滤器_SSH中的过滤器,拦截器,监听器的一些基本认识
  4. Ext JS 6开发实例(四) :调整主视图
  5. alexnet训练多久收敛_AlexNet浅析
  6. 前端面试题集锦(一)之HTML部分
  7. 网站计数器 web映射
  8. html中点击照片时放大缩小,基于jquery实现一张图片点击鼠标放大再点缩小
  9. 怎么改变asp.net中.sln文件的默认生成路径
  10. Tigase数据库结构(1)
  11. Luogu1886 滑动窗口 /【模板】单调队列
  12. revit2016与2017区别_revit2017下载及新功能介绍
  13. 21、2010年中兴面试题
  14. 油管机器学习基础 学习笔记
  15. Can‘t update has no tracked branch
  16. 固定效应还是随机效应?
  17. ucenter php7.0版,UCenter1.5.0UCenter Home1.5Discuz! 7.0 集成安装包
  18. C语言编写程序求1到100的和,C语言菜鸟基础教程之求1到100的和
  19. Linux目录以及文件权限理解
  20. 2018国内各大互联网公司前端面试题汇总【转:公众号~~高级前端进阶公众号】

热门文章

  1. win8的计算机在哪个地方,Win8系统中的关机在电脑哪个位置
  2. python怎么改变字体大小_如何在matplotlib图上更改字体大小
  3. getopt()函数详解
  4. 计算机应用基础》模拟考试卷一,《计算机应用基础》12级模拟考试试卷一
  5. A Systematic Evaluation of Transient Execution Attacks and Defenses (对暂态执行的攻击和防御的系统评估)(前四节)
  6. 【C语言】 -- 三子棋(代码+解析)
  7. Openwrt旁路由设置vs无法打开国内网站解决方法。
  8. 长文对话实录:国内物联网10年沉浮,AIoT技术如何破局?| AIoT+智慧城市峰会...
  9. Java 程序中的指令重排
  10. 引导宝宝好状态拍摄?