Swift教程之循环语句

前端之家收集整理的这篇文章主要介绍了Swift教程之循环语句前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
import Foundation

//MARK:-----for循环
//for-in
for index in 1...5
{
    print("\(index) * 5 = \(index*5) ")
}

//求3的10次幂
//如果不需要知道区间内每一项的值,你可以使用下划线(_)
var answer = 1
for _ in 1...10
{
    answer *= 3
}
print("3的10次幂是\(answer)")

//for-in 遍历数组
let names = ["小米","苹果"]
for name in names
{
    print("Hello,\(name)!")
}

//for-in 遍历字典
let dic = ["地址":"北京","age":20]
for (key,value) in dic
{
    print("\(key)是\(value)")
}

//for条件递增(for-condition-increment)
for var i = 0; i < 3; ++i
{
    print("i is \(i)")
}

//MARK:----while循环和do-while循环
//while循环
var va = 1
while va < 5
{
    print("while index is \(va)")
    va++
}


//do-while循环
var vb = 1
repeat
{
    print("do while index is \(vb)")
    vb++
} while vb < 5
原文链接:https://www.f2er.com/swift/326669.html

猜你在找的Swift相关文章